Skip to content

Commit 714f75c

Browse files
committed
include support for liquid-node
1 parent bcc3cff commit 714f75c

File tree

6 files changed

+36
-45
lines changed

6 files changed

+36
-45
lines changed

lib/consolidate.js

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -189,36 +189,23 @@ exports.liquid = fromStringRenderer('liquid');
189189

190190
exports.liquid.render = function(str, options, fn){
191191
return promisify(fn, function (fn) {
192-
var engine = requires.liquid || (requires.liquid = require('tinyliquid'));
192+
var Liquid = requires.liquid || (requires.liquid = require('liquid-node'));
193+
var engine = new Liquid.Engine;
194+
193195
try {
194-
var context = engine.newContext();
195-
var k;
196-
197-
/**
198-
* Note that there's a bug in the library that doesn't allow us to pass
199-
* the locals to newContext(), hence looping through the keys:
200-
*/
201-
202-
if (options.locals){
203-
for (k in options.locals){
204-
context.setLocals(k, options.locals[k]);
205-
}
206-
delete options.locals;
207-
}
196+
var locals = options.locals || {};
208197

209198
if (options.meta){
210-
context.setLocals('page', options.meta);
199+
locals.pages = options.meta;
211200
delete options.meta;
212201
}
213202

214203
/**
215204
* Add any defined filters:
216205
*/
217-
218-
if (options.filters){
219-
for (k in options.filters){
220-
context.setFilter(k, options.filters[k]);
221-
}
206+
207+
if (options.filters) {
208+
engine.registerFilters(options.filters);
222209
delete options.filters;
223210
}
224211

@@ -227,16 +214,7 @@ exports.liquid.render = function(str, options, fn){
227214
*/
228215

229216
var includeDir = options.includeDir || process.cwd();
230-
231-
context.onInclude(function (name, callback) {
232-
var extname = path.extname(name) ? '' : '.liquid';
233-
var filename = path.resolve(includeDir, name + extname);
234-
235-
fs.readFile(filename, {encoding: 'utf8'}, function (err, data){
236-
if (err) return callback(err);
237-
callback(null, engine.parse(data));
238-
});
239-
});
217+
engine.fileSystem = new Liquid.LocalFileSystem(includeDir, 'liquid');
240218
delete options.includeDir;
241219

242220
/**
@@ -251,14 +229,15 @@ exports.liquid.render = function(str, options, fn){
251229

252230
if (options.customTags){
253231
var tagFunctions = options.customTags;
254-
232+
255233
for (k in options.customTags){
234+
engine.registerTag(k, tagFunctions[k]);
256235
/*Tell jshint there's no problem with having this function in the loop */
257236
/*jshint -W083 */
258-
compileOptions.customTags[k] = function (context, name, body){
259-
var tpl = tagFunctions[name](body.trim());
260-
context.astStack.push(engine.parse(tpl));
261-
};
237+
// compileOptions.customTags[k] = function (context, name, body){
238+
// var tpl = tagFunctions[name](body.trim());
239+
// context.astStack.push(engine.parse(tpl));
240+
// };
262241
/*jshint +W083 */
263242
}
264243
delete options.customTags;
@@ -268,16 +247,27 @@ exports.liquid.render = function(str, options, fn){
268247
* Now anything left in `options` becomes a local:
269248
*/
270249

271-
for (k in options){
272-
context.setLocals(k, options[k]);
250+
for (var k in options){
251+
locals[k] = options[k];
273252
}
274253

275254
/**
276255
* Finally, execute the template:
277256
*/
257+
258+
return engine
259+
.parseAndRender(str, locals)
260+
.nodeify(function (err, result) {
261+
if (err) {
262+
throw new Error(err);
263+
} else {
264+
return fn(null, result);
265+
}
266+
});
267+
268+
// var tmpl = cache(context) || cache(context, engine.compile(str, compileOptions));
269+
// tmpl(context, fn);
278270

279-
var tmpl = cache(context) || cache(context, engine.compile(str, compileOptions));
280-
tmpl(context, fn);
281271
} catch (err) {
282272
fn(err);
283273
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
"test": "mocha"
6060
},
6161
"dependencies": {
62-
"bluebird": "^3.1.1"
62+
"bluebird": "^3.1.1",
63+
"liquid-node": "^2.6.1",
64+
"slm": "^0.5.0"
6365
}
6466
}
File renamed without changes.

test/fixtures/liquid/include.liquid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{% include user %}
22
{% include sub/file %}
3-
{% include "footer.html" %}
3+
{% include "footer.liquid" %}

test/shared/filters.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exports.test = function(name) {
1313
var str = fs.readFileSync('test/fixtures/' + name + '/filters.' + name).toString();
1414

1515
var locals = { user: user, filters: { toupper: function(object) {
16-
return object.toUpperCase();
16+
return String(object).toUpperCase();
1717
}}};
1818

1919
cons[name].render(str, locals, function(err, html){

test/shared/includes.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ exports.test = function(name) {
1010

1111
it('should support includes', function(done) {
1212
var str = fs.readFileSync('test/fixtures/' + name + '/include.' + name).toString();
13-
1413
var locals = { user: user, includeDir: 'test/fixtures/' + name };
1514

1615
cons[name].render(str, locals, function(err, html){
1716
if (err) return done(err);
1817
try{
1918
if (name === 'liquid') {
20-
html.should.eql('<p>Tobi</p><section></section><footer></footer>');
19+
html.should.eql('<p>Tobi</p>\n<section></section>\n<footer></footer>');
2120
} else {
2221
html.should.eql('<p>Tobi</p>');
2322
}

0 commit comments

Comments
 (0)