|
38 | 38 |
|
39 | 39 | if(partials) {
|
40 | 40 | return mustache.render(template, data, partials);
|
41 |
| - }else{ |
| 41 | + } else{ |
42 | 42 | return mustache.render(template, data);
|
43 | 43 | }
|
44 | 44 | }
|
|
92 | 92 | lineage_hunter = new lh(),
|
93 | 93 | pseudopattern_hunter = new pph();
|
94 | 94 |
|
| 95 | + currentPattern.extendedTemplate = currentPattern.template; |
| 96 | + |
95 | 97 | //find how many partials there may be for the given pattern
|
96 | 98 | var foundPatternPartials = findPartials(currentPattern);
|
97 | 99 |
|
98 | 100 | if(foundPatternPartials !== null && foundPatternPartials.length > 0){
|
99 | 101 |
|
100 |
| - console.log(foundPatternPartials); |
101 |
| - |
| 102 | + if(patternlab.config.debug){ |
| 103 | + console.log('found partials for ' + currentPattern.key); |
| 104 | + } |
102 | 105 | //determine if the template contains any pattern parameters. if so they must be immediately consumed
|
103 | 106 | var patternsConsumedWithParameters = parameter_hunter.find_parameters(currentPattern, patternlab);
|
104 | 107 |
|
105 |
| - if(patternsConsumedWithParameters === 0){ |
106 |
| - //do something with the regular old partials |
| 108 | + //do something with the regular old partials |
| 109 | + for(var i = 0; i < foundPatternPartials.length; i++){ |
| 110 | + var partialKey = foundPatternPartials[i].replace(/{{>([ ])?([A-Za-z0-9-]+)(?:\:[A-Za-z0-9-]+)?(?:(| )\(.*)?([ ])?}}/g, '$2'); |
| 111 | + console.log('key for partial is ' + partialKey); |
| 112 | + var partialPattern = getpatternbykey(partialKey, patternlab); |
| 113 | + currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPatternPartials[i], partialPattern.extendedTemplate); |
107 | 114 | }
|
108 | 115 |
|
109 | 116 | } else{
|
110 | 117 | //we found no partials, so we are ready to render
|
111 |
| - |
| 118 | + if(patternlab.config.debug){ |
| 119 | + console.log('no partial found in pattern ' + currentPattern.key); |
| 120 | + } |
112 | 121 | }
|
113 | 122 |
|
114 | 123 | //find pattern lineage
|
115 | 124 | lineage_hunter.find_lineage(currentPattern, patternlab);
|
116 | 125 |
|
117 | 126 | //add as a partial in case this is referenced later. convert to syntax needed by existing patterns
|
118 |
| - var sub = currentPattern.subdir.substring(currentPattern.subdir.indexOf('-') + 1); |
119 |
| - var folderIndex = sub.indexOf(path.sep); |
120 |
| - var cleanSub = sub.substring(0, folderIndex); |
121 |
| - |
122 |
| - //add any templates found to an object of partials, so downstream templates may use them too |
123 |
| - //look for the full path on nested patterns, else expect it to be flat |
124 |
| - var partialname = ''; |
125 |
| - if(cleanSub !== ''){ |
126 |
| - partialname = cleanSub + '-' + currentPattern.patternName; |
127 |
| - } else{ |
128 |
| - partialname = currentPattern.patternGroup + '-' + currentPattern.patternName; |
129 |
| - } |
130 |
| - patternlab.partials[partialname] = currentPattern.template; |
| 127 | + // var sub = currentPattern.subdir.substring(currentPattern.subdir.indexOf('-') + 1); |
| 128 | + // var folderIndex = sub.indexOf(path.sep); |
| 129 | + // var cleanSub = sub.substring(0, folderIndex); |
| 130 | + // |
| 131 | + // //add any templates found to an object of partials, so downstream templates may use them too |
| 132 | + // //look for the full path on nested patterns, else expect it to be flat |
| 133 | + // var partialname = ''; |
| 134 | + // if(cleanSub !== ''){ |
| 135 | + // partialname = cleanSub + '-' + currentPattern.patternName; |
| 136 | + // } else{ |
| 137 | + // partialname = currentPattern.patternGroup + '-' + currentPattern.patternName; |
| 138 | + // } |
| 139 | + // patternlab.partials[partialname] = currentPattern.template; |
131 | 140 |
|
132 | 141 | //look for a pseudo pattern by checking if there is a file containing same name, with ~ in it, ending in .json
|
133 | 142 | pseudopattern_hunter.find_pseudopatterns(currentPattern, patternlab);
|
|
136 | 145 | addPattern(currentPattern, patternlab);
|
137 | 146 | }
|
138 | 147 |
|
| 148 | + function getpatternbykey(key, patternlab){ |
| 149 | + for(var i = 0; i < patternlab.patterns.length; i++){ |
| 150 | + if(patternlab.patterns[i].key === key){ |
| 151 | + return patternlab.patterns[i]; |
| 152 | + } |
| 153 | + } |
| 154 | + throw 'Could not find pattern with key ' + key; |
| 155 | + } |
| 156 | + |
| 157 | + /* |
| 158 | + * Recursively merge properties of two objects |
| 159 | + * http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically |
| 160 | + */ |
| 161 | + function mergeData(obj1, obj2) { |
| 162 | + for (var p in obj2) { |
| 163 | + try { |
| 164 | + // Property in destination object set; update its value. |
| 165 | + if ( obj2[p].constructor == Object ) { |
| 166 | + obj1[p] = merge_data(obj1[p], obj2[p]); |
| 167 | + |
| 168 | + } else { |
| 169 | + obj1[p] = obj2[p]; |
| 170 | + |
| 171 | + } |
| 172 | + } catch(e) { |
| 173 | + // Property in destination object not set; create it and set its value. |
| 174 | + obj1[p] = obj2[p]; |
| 175 | + } |
| 176 | + } |
| 177 | + return obj1; |
| 178 | + } |
| 179 | + |
139 | 180 | return {
|
140 | 181 | find_pattern_partials: function(pattern){
|
141 | 182 | return findPartials(pattern);
|
|
154 | 195 | },
|
155 | 196 | process_pattern: function(pattern, patternlab, additionalData){
|
156 | 197 | processPattern(pattern, patternlab, additionalData);
|
| 198 | + }, |
| 199 | + get_pattern_by_key: function(key, patternlab){ |
| 200 | + return getpatternbykey(key, patternlab); |
| 201 | + }, |
| 202 | + merge_data: function(existingData, newData){ |
| 203 | + return mergeData(existingData, newData); |
157 | 204 | }
|
158 | 205 | };
|
159 | 206 |
|
|
0 commit comments