Skip to content

Commit 14a873f

Browse files
committed
Abstracted template rendering; fixed issue with JSON being after template in filesystem; cleaned up render function
1 parent 8b4f0c6 commit 14a873f

File tree

1 file changed

+63
-77
lines changed

1 file changed

+63
-77
lines changed

builder/patternlab.js

Lines changed: 63 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* patternlab-node - v0.1.2 - 2014-07-12
2+
* patternlab-node - v0.1.2 - 2014-07-15
33
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.
@@ -61,87 +61,65 @@ var patternlab_engine = function(grunt){
6161
var currentPattern;
6262
var flatPatternPath;
6363

64-
//ignore _underscored patterns
65-
if(filename.charAt(0) === '_'){
64+
//ignore _underscored patterns and json
65+
if(filename.charAt(0) === '_' || grunt.util._.str.include(filename, 'json')){
6666
return;
6767
}
68+
69+
70+
//make a new Pattern Object
71+
var flatPatternName = subdir.replace(/\//g, '-') + '-' + patternName;
72+
flatPatternName = flatPatternName.replace(/\//g, '-');
73+
currentPattern = new of.oPattern(flatPatternName, subdir, filename, {});
74+
currentPattern.patternName = patternName.substring(patternName.indexOf('-') + 1);
75+
currentPattern.data = null;
76+
77+
//look for a json file for this template
78+
try {
79+
var jsonFilename = abspath.substr(0, abspath.lastIndexOf(".")) + ".json";
80+
currentPattern.data = grunt.file.readJSON(jsonFilename);
81+
}
82+
catch(e) {
6883

69-
//two reasons could return no pattern, 1) just a bare mustache, or 2) a json found before the mustache
70-
//returns -1 if patterns does not exist, otherwise returns the index
71-
//add the pattern array if first time, otherwise pull it up
72-
if(patternIndex === -1){
73-
grunt.verbose.ok('pattern not found, adding to array');
74-
var flatPatternName = subdir.replace(/\//g, '-') + '-' + patternName;
75-
flatPatternName = flatPatternName.replace(/\//g, '-');
76-
currentPattern = new of.oPattern(flatPatternName, subdir, filename, {});
77-
currentPattern.patternName = patternName.substring(patternName.indexOf('-') + 1);
78-
79-
if(grunt.util._.str.include(filename, 'json')){
80-
grunt.verbose.ok('json file found first, so add it to the pattern and continuing');
81-
currentPattern.data = grunt.file.readJSON(abspath);
82-
//done
83-
} else{
84-
grunt.verbose.ok('mustache file found, assume no data, so compile it right away');
85-
currentPattern.template = grunt.file.read(abspath);
86-
87-
//render the pattern. pass partials object just in case.
88-
currentPattern.patternPartial = mustache.render(currentPattern.template, patternlab.data, patternlab.partials);
89-
90-
//write the compiled template to the public patterns directory
91-
flatPatternPath = currentPattern.name + '/' + currentPattern.name + '.html';
92-
93-
//add footer info before writing
94-
var currentPatternFooter = mustache.render(patternlab.footer, currentPattern);
95-
96-
grunt.file.write('./public/patterns/' + flatPatternPath, patternlab.header + currentPattern.patternPartial + currentPatternFooter);
97-
currentPattern.patternLink = flatPatternPath;
98-
99-
//add as a partial in case this is referenced later. convert to syntax needed by existing patterns
100-
var sub = subdir.substring(subdir.indexOf('-') + 1);
101-
var folderIndex = sub.indexOf('/'); //THIS IS MOST LIKELY WINDOWS ONLY. path.sep not working yet
102-
var cleanSub = sub.substring(0, folderIndex);
103-
104-
//add any templates found to an object of partials, so downstream templates may use them too
105-
//exclude the template patterns - we don't need them as partials because pages will just swap data
106-
if(cleanSub !== ''){
107-
var partialname = cleanSub + '-' + patternName.substring(patternName.indexOf('-') + 1);
108-
109-
patternlab.partials[partialname] = currentPattern.template;
84+
}
11085

111-
//done
112-
}
113-
}
114-
//add to patternlab arrays so we can look these up later. this could probably just be an object.
115-
patternlab.patternIndex.push(currentPattern.name);
116-
patternlab.patterns.push(currentPattern);
117-
} else{
118-
//if we get here, we can almost ensure we found the json first, so render the template and pass in the unique json
119-
currentPattern = patternlab.patterns[patternIndex];
120-
grunt.verbose.ok('pattern found, loaded');
121-
//determine if this file is data or pattern
122-
if(grunt.util._.str.include(filename, 'mustache')){
86+
currentPattern.template = grunt.file.read(abspath);
12387

124-
currentPattern.template = grunt.file.read(abspath);
88+
//render the pattern. pass partials object just in case.
89+
if(currentPattern.data) { // Pass JSON as data
90+
currentPattern.patternPartial = renderPattern(currentPattern.template, currentPattern.data, patternlab.partials);
91+
}else{ // Pass global patternlab data
92+
currentPattern.patternPartial = renderPattern(currentPattern.template, patternlab.data, patternlab.partials);
93+
}
94+
95+
//write the compiled template to the public patterns directory
96+
flatPatternPath = currentPattern.name + '/' + currentPattern.name + '.html';
12597

126-
//render the pattern. pass partials object just in case.
127-
currentPattern.patternPartial = mustache.render(currentPattern.template, currentPattern.data, patternlab.partials);
128-
grunt.verbose.ok('template compiled with data!');
98+
//add footer info before writing
99+
var currentPatternFooter = renderPattern(patternlab.footer, currentPattern);
129100

130-
//write the compiled template to the public patterns directory
131-
flatPatternPath = currentPattern.name + '/' + currentPattern.name + '.html';
101+
grunt.file.write('./public/patterns/' + flatPatternPath, patternlab.header + currentPattern.patternPartial + currentPatternFooter);
102+
currentPattern.patternLink = flatPatternPath;
132103

133-
//add footer info before writing
134-
var currentPatternFooter = mustache.render(patternlab.footer, currentPattern);
104+
//add as a partial in case this is referenced later. convert to syntax needed by existing patterns
105+
var sub = subdir.substring(subdir.indexOf('-') + 1);
106+
var folderIndex = sub.indexOf('/'); //THIS IS MOST LIKELY WINDOWS ONLY. path.sep not working yet
107+
var cleanSub = sub.substring(0, folderIndex);
135108

136-
grunt.file.write('./public/patterns/' + flatPatternPath, patternlab.header + currentPattern.patternPartial + currentPatternFooter);
109+
//add any templates found to an object of partials, so downstream templates may use them too
110+
//exclude the template patterns - we don't need them as partials because pages will just swap data
111+
if(cleanSub !== ''){
112+
var partialname = cleanSub + '-' + patternName.substring(patternName.indexOf('-') + 1);
137113

138-
currentPattern.patternLink = flatPatternPath;
114+
patternlab.partials[partialname] = currentPattern.template;
115+
hbs.registerPartial(partialname, currentPattern.template);
139116

140-
//done
141-
} else{
142-
grunt.log.error('json encountered!? there should only be one');
143-
}
117+
//done
144118
}
119+
120+
//add to patternlab arrays so we can look these up later. this could probably just be an object.
121+
patternlab.patternIndex.push(currentPattern.name);
122+
patternlab.patterns.push(currentPattern);
145123

146124
});
147125

@@ -155,7 +133,7 @@ var patternlab_engine = function(grunt){
155133

156134
//build the styleguide
157135
var styleguideTemplate = grunt.file.read('./source/_patternlab-files/styleguide.mustache');
158-
var styleguideHtml = mustache.render(styleguideTemplate, {partials: patternlab.patterns});
136+
var styleguideHtml = renderPattern(styleguideTemplate, {partials: patternlab.patterns});
159137
grunt.file.write('./public/styleguide/html/styleguide.html', styleguideHtml);
160138

161139
//build the patternlab website
@@ -285,29 +263,29 @@ var patternlab_engine = function(grunt){
285263
//the patternlab site requires a lot of partials to be rendered.
286264
//patternNav
287265
var patternNavTemplate = grunt.file.read('./source/_patternlab-files/partials/patternNav.mustache');
288-
var patternNavPartialHtml = mustache.render(patternNavTemplate, patternlab);
266+
var patternNavPartialHtml = renderPattern(patternNavTemplate, patternlab);
289267

290268
//ishControls
291269
var ishControlsTemplate = grunt.file.read('./source/_patternlab-files/partials/ishControls.mustache');
292-
var ishControlsPartialHtml = mustache.render(ishControlsTemplate, patternlab.config);
270+
var ishControlsPartialHtml = renderPattern(ishControlsTemplate, patternlab.config);
293271

294272
//patternPaths
295273
var patternPathsTemplate = grunt.file.read('./source/_patternlab-files/partials/patternPaths.mustache');
296-
var patternPathsPartialHtml = mustache.render(patternPathsTemplate, {'patternPaths': JSON.stringify(patternlab.patternPaths)});
274+
var patternPathsPartialHtml = renderPattern(patternPathsTemplate, {'patternPaths': JSON.stringify(patternlab.patternPaths)});
297275

298276
//viewAllPaths
299277
var viewAllPathsTemplate = grunt.file.read('./source/_patternlab-files/partials/viewAllPaths.mustache');
300-
var viewAllPathersPartialHtml = mustache.render(viewAllPathsTemplate, {'viewallpaths': JSON.stringify(patternlab.viewAllPaths)});
278+
var viewAllPathersPartialHtml = renderPattern(viewAllPathsTemplate, {'viewallpaths': JSON.stringify(patternlab.viewAllPaths)});
301279

302280
//websockets
303281
var websocketsTemplate = grunt.file.read('./source/_patternlab-files/partials/websockets.mustache');
304282
patternlab.contentsyncport = patternlab.config.contentSyncPort;
305283
patternlab.navsyncport = patternlab.config.navSyncPort;
306284

307-
var websocketsPartialHtml = mustache.render(websocketsTemplate, patternlab);
285+
var websocketsPartialHtml = renderPattern(websocketsTemplate, patternlab);
308286

309287
//render the patternlab template, with all partials
310-
var patternlabSiteHtml = mustache.render(patternlabSiteTemplate, {}, {
288+
var patternlabSiteHtml = renderPattern(patternlabSiteTemplate, {}, {
311289
'ishControls': ishControlsPartialHtml,
312290
'patternNav': patternNavPartialHtml,
313291
'patternPaths': patternPathsPartialHtml,
@@ -318,6 +296,14 @@ var patternlab_engine = function(grunt){
318296

319297
}
320298

299+
function renderPattern(name, data, partials) {
300+
if(partials) {
301+
return mustache.render(name, data, partials);
302+
}else{
303+
return mustache.render(name, data);
304+
}
305+
}
306+
321307
return {
322308
version: function(){
323309
return getVersion();

0 commit comments

Comments
 (0)