@@ -121,14 +121,14 @@ function readPartials(path, options, fn) {
121
121
if ( index === keys . length ) return fn ( null ) ;
122
122
var key = keys [ index ] ;
123
123
var partialPath = partials [ key ] ;
124
-
124
+
125
125
var file ;
126
126
if ( isAbsolute ( partialPath ) ) {
127
127
if ( extname ( partialPath ) !== '' ) {
128
128
file = partialPath ;
129
129
} else {
130
130
file = join ( partialPath + extname ( path ) ) ;
131
- }
131
+ }
132
132
} else {
133
133
file = join ( dirname ( path ) , partialPath + extname ( path ) ) ;
134
134
}
@@ -202,38 +202,137 @@ exports.liquid = fromStringRenderer('liquid');
202
202
* `includeDir` will also become a local.
203
203
*/
204
204
205
+ function _renderTinyliquid ( engine , str , options , fn ) {
206
+ var context = engine . newContext ( ) ;
207
+ var k ;
208
+
209
+ /**
210
+ * Note that there's a bug in the library that doesn't allow us to pass
211
+ * the locals to newContext(), hence looping through the keys:
212
+ */
213
+
214
+ if ( options . locals ) {
215
+ for ( k in options . locals ) {
216
+ context . setLocals ( k , options . locals [ k ] ) ;
217
+ }
218
+ delete options . locals ;
219
+ }
220
+
221
+ if ( options . meta ) {
222
+ context . setLocals ( 'page' , options . meta ) ;
223
+ delete options . meta ;
224
+ }
225
+
226
+ /**
227
+ * Add any defined filters:
228
+ */
229
+
230
+ if ( options . filters ) {
231
+ for ( k in options . filters ) {
232
+ context . setFilter ( k , options . filters [ k ] ) ;
233
+ }
234
+ delete options . filters ;
235
+ }
236
+
237
+ /**
238
+ * Set up a callback for the include directory:
239
+ */
240
+
241
+ var includeDir = options . includeDir || process . cwd ( ) ;
242
+
243
+ context . onInclude ( function ( name , callback ) {
244
+ var extname = path . extname ( name ) ? '' : '.liquid' ;
245
+ var filename = path . resolve ( includeDir , name + extname ) ;
246
+
247
+ fs . readFile ( filename , { encoding : 'utf8' } , function ( err , data ) {
248
+ if ( err ) return callback ( err ) ;
249
+ callback ( null , engine . parse ( data ) ) ;
250
+ } ) ;
251
+ } ) ;
252
+ delete options . includeDir ;
253
+
254
+ /**
255
+ * The custom tag functions need to have their results pushed back
256
+ * through the parser, so set up a shim before calling the provided
257
+ * callback:
258
+ */
259
+
260
+ var compileOptions = {
261
+ customTags : { }
262
+ } ;
263
+
264
+ if ( options . customTags ) {
265
+ var tagFunctions = options . customTags ;
266
+
267
+ for ( k in options . customTags ) {
268
+ /*Tell jshint there's no problem with having this function in the loop */
269
+ /*jshint -W083 */
270
+ compileOptions . customTags [ k ] = function ( context , name , body ) {
271
+ var tpl = tagFunctions [ name ] ( body . trim ( ) ) ;
272
+ context . astStack . push ( engine . parse ( tpl ) ) ;
273
+ } ;
274
+ /*jshint +W083 */
275
+ }
276
+ delete options . customTags ;
277
+ }
278
+
279
+ /**
280
+ * Now anything left in `options` becomes a local:
281
+ */
282
+
283
+ for ( k in options ) {
284
+ context . setLocals ( k , options [ k ] ) ;
285
+ }
286
+
287
+ /**
288
+ * Finally, execute the template:
289
+ */
290
+
291
+ var tmpl = cache ( context ) || cache ( context , engine . compile ( str , compileOptions ) ) ;
292
+ tmpl ( context , fn ) ;
293
+ }
294
+
205
295
exports . liquid . render = function ( str , options , fn ) {
206
296
return promisify ( fn , function ( fn ) {
207
- var engine = requires . liquid || ( requires . liquid = require ( 'tinyliquid' ) ) ;
297
+ var engine = requires . liquid ;
298
+ var Liquid ;
299
+
208
300
try {
209
- var context = engine . newContext ( ) ;
210
- var k ;
301
+ // set up tinyliquid engine
302
+ engine = requires . liquid = require ( 'tinyliquid' ) ;
211
303
212
- /**
213
- * Note that there's a bug in the library that doesn't allow us to pass
214
- * the locals to newContext(), hence looping through the keys:
215
- */
304
+ // use tinyliquid engine
305
+ _renderTinyliquid ( engine , str , options , fn ) ;
216
306
217
- if ( options . locals ) {
218
- for ( k in options . locals ) {
219
- context . setLocals ( k , options . locals [ k ] ) ;
220
- }
221
- delete options . locals ;
307
+ return ;
308
+
309
+ } catch ( err ) {
310
+
311
+ // set up liquid-node engine
312
+ try {
313
+ Liquid = requires . liquid = require ( 'liquid-node' ) ;
314
+ engine = new Liquid . Engine ;
315
+ } catch ( err ) {
316
+ throw err ;
222
317
}
223
318
319
+ }
320
+
321
+ // use liquid-node engine
322
+ try {
323
+ var locals = options . locals || { } ;
324
+
224
325
if ( options . meta ) {
225
- context . setLocals ( 'page' , options . meta ) ;
326
+ locals . pages = options . meta ;
226
327
delete options . meta ;
227
328
}
228
329
229
330
/**
230
331
* Add any defined filters:
231
332
*/
232
333
233
- if ( options . filters ) {
234
- for ( k in options . filters ) {
235
- context . setFilter ( k , options . filters [ k ] ) ;
236
- }
334
+ if ( options . filters ) {
335
+ engine . registerFilters ( options . filters ) ;
237
336
delete options . filters ;
238
337
}
239
338
@@ -242,16 +341,7 @@ exports.liquid.render = function(str, options, fn){
242
341
*/
243
342
244
343
var includeDir = options . includeDir || process . cwd ( ) ;
245
-
246
- context . onInclude ( function ( name , callback ) {
247
- var extname = path . extname ( name ) ? '' : '.liquid' ;
248
- var filename = path . resolve ( includeDir , name + extname ) ;
249
-
250
- fs . readFile ( filename , { encoding : 'utf8' } , function ( err , data ) {
251
- if ( err ) return callback ( err ) ;
252
- callback ( null , engine . parse ( data ) ) ;
253
- } ) ;
254
- } ) ;
344
+ engine . fileSystem = new Liquid . LocalFileSystem ( includeDir , 'liquid' ) ;
255
345
delete options . includeDir ;
256
346
257
347
/**
@@ -268,13 +358,7 @@ exports.liquid.render = function(str, options, fn){
268
358
var tagFunctions = options . customTags ;
269
359
270
360
for ( k in options . customTags ) {
271
- /*Tell jshint there's no problem with having this function in the loop */
272
- /*jshint -W083 */
273
- compileOptions . customTags [ k ] = function ( context , name , body ) {
274
- var tpl = tagFunctions [ name ] ( body . trim ( ) ) ;
275
- context . astStack . push ( engine . parse ( tpl ) ) ;
276
- } ;
277
- /*jshint +W083 */
361
+ engine . registerTag ( k , tagFunctions [ k ] ) ;
278
362
}
279
363
delete options . customTags ;
280
364
}
@@ -283,16 +367,24 @@ exports.liquid.render = function(str, options, fn){
283
367
* Now anything left in `options` becomes a local:
284
368
*/
285
369
286
- for ( k in options ) {
287
- context . setLocals ( k , options [ k ] ) ;
370
+ for ( var k in options ) {
371
+ locals [ k ] = options [ k ] ;
288
372
}
289
373
290
374
/**
291
375
* Finally, execute the template:
292
376
*/
293
377
294
- var tmpl = cache ( context ) || cache ( context , engine . compile ( str , compileOptions ) ) ;
295
- tmpl ( context , fn ) ;
378
+ return engine
379
+ . parseAndRender ( str , locals )
380
+ . nodeify ( function ( err , result ) {
381
+ if ( err ) {
382
+ throw new Error ( err ) ;
383
+ } else {
384
+ return fn ( null , result ) ;
385
+ }
386
+ } ) ;
387
+
296
388
} catch ( err ) {
297
389
fn ( err ) ;
298
390
}
0 commit comments