@@ -187,11 +187,123 @@ exports.liquid = fromStringRenderer('liquid');
187
187
* `includeDir` will also become a local.
188
188
*/
189
189
190
+ function _renderTinyliquid ( engine , str , options , fn ) {
191
+ var context = engine . newContext ( ) ;
192
+ var k ;
193
+
194
+ /**
195
+ * Note that there's a bug in the library that doesn't allow us to pass
196
+ * the locals to newContext(), hence looping through the keys:
197
+ */
198
+
199
+ if ( options . locals ) {
200
+ for ( k in options . locals ) {
201
+ context . setLocals ( k , options . locals [ k ] ) ;
202
+ }
203
+ delete options . locals ;
204
+ }
205
+
206
+ if ( options . meta ) {
207
+ context . setLocals ( 'page' , options . meta ) ;
208
+ delete options . meta ;
209
+ }
210
+
211
+ /**
212
+ * Add any defined filters:
213
+ */
214
+
215
+ if ( options . filters ) {
216
+ for ( k in options . filters ) {
217
+ context . setFilter ( k , options . filters [ k ] ) ;
218
+ }
219
+ delete options . filters ;
220
+ }
221
+
222
+ /**
223
+ * Set up a callback for the include directory:
224
+ */
225
+
226
+ var includeDir = options . includeDir || process . cwd ( ) ;
227
+
228
+ context . onInclude ( function ( name , callback ) {
229
+ var extname = path . extname ( name ) ? '' : '.liquid' ;
230
+ var filename = path . resolve ( includeDir , name + extname ) ;
231
+
232
+ fs . readFile ( filename , { encoding : 'utf8' } , function ( err , data ) {
233
+ if ( err ) return callback ( err ) ;
234
+ callback ( null , engine . parse ( data ) ) ;
235
+ } ) ;
236
+ } ) ;
237
+ delete options . includeDir ;
238
+
239
+ /**
240
+ * The custom tag functions need to have their results pushed back
241
+ * through the parser, so set up a shim before calling the provided
242
+ * callback:
243
+ */
244
+
245
+ var compileOptions = {
246
+ customTags : { }
247
+ } ;
248
+
249
+ if ( options . customTags ) {
250
+ var tagFunctions = options . customTags ;
251
+
252
+ for ( k in options . customTags ) {
253
+ /*Tell jshint there's no problem with having this function in the loop */
254
+ /*jshint -W083 */
255
+ compileOptions . customTags [ k ] = function ( context , name , body ) {
256
+ var tpl = tagFunctions [ name ] ( body . trim ( ) ) ;
257
+ context . astStack . push ( engine . parse ( tpl ) ) ;
258
+ } ;
259
+ /*jshint +W083 */
260
+ }
261
+ delete options . customTags ;
262
+ }
263
+
264
+ /**
265
+ * Now anything left in `options` becomes a local:
266
+ */
267
+
268
+ for ( k in options ) {
269
+ context . setLocals ( k , options [ k ] ) ;
270
+ }
271
+
272
+ /**
273
+ * Finally, execute the template:
274
+ */
275
+
276
+ var tmpl = cache ( context ) || cache ( context , engine . compile ( str , compileOptions ) ) ;
277
+ tmpl ( context , fn ) ;
278
+ }
279
+
190
280
exports . liquid . render = function ( str , options , fn ) {
191
281
return promisify ( fn , function ( fn ) {
192
- var Liquid = requires . liquid || ( requires . liquid = require ( 'liquid-node' ) ) ;
193
- var engine = new Liquid . Engine ;
282
+ var engine = requires . liquid ;
283
+ var Liquid ;
284
+
285
+ try {
286
+ // set up tinyliquid engine
287
+ engine = requires . liquid = require ( 'tinyliquid' ) ;
288
+
289
+ // use tinyliquid engine
290
+ _renderTinyliquid ( engine , str , options , fn ) ;
291
+
292
+ return ;
293
+
294
+ } catch ( err ) {
295
+
296
+ // set up liquid-node engine
297
+ try {
298
+ Liquid = requires . liquid = require ( 'liquid-node' ) ;
299
+ engine = new Liquid . Engine ;
300
+ } catch ( err ) {
301
+ throw err ;
302
+ }
303
+
304
+ }
194
305
306
+ // use liquid-node engine
195
307
try {
196
308
var locals = options . locals || { } ;
197
309
@@ -232,13 +344,6 @@ exports.liquid.render = function(str, options, fn){
232
344
233
345
for ( k in options . customTags ) {
234
346
engine . registerTag ( k , tagFunctions [ k ] ) ;
235
- /*Tell jshint there's no problem with having this function in the loop */
236
- /*jshint -W083 */
237
- // compileOptions.customTags[k] = function (context, name, body){
238
- // var tpl = tagFunctions[name](body.trim());
239
- // context.astStack.push(engine.parse(tpl));
240
- // };
241
- /*jshint +W083 */
242
347
}
243
348
delete options . customTags ;
244
349
}
@@ -265,9 +370,6 @@ exports.liquid.render = function(str, options, fn){
265
370
}
266
371
} ) ;
267
372
268
- // var tmpl = cache(context) || cache(context, engine.compile(str, compileOptions));
269
- // tmpl(context, fn);
270
-
271
373
} catch ( err ) {
272
374
fn ( err ) ;
273
375
}
0 commit comments