Skip to content

Commit

Permalink
include support for liquid-node
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron J Roe committed Jan 25, 2016
1 parent bcc3cff commit 714f75c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 45 deletions.
70 changes: 30 additions & 40 deletions lib/consolidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,36 +189,23 @@ exports.liquid = fromStringRenderer('liquid');

exports.liquid.render = function(str, options, fn){
return promisify(fn, function (fn) {
var engine = requires.liquid || (requires.liquid = require('tinyliquid'));
var Liquid = requires.liquid || (requires.liquid = require('liquid-node'));
var engine = new Liquid.Engine;

try {
var context = engine.newContext();
var k;

/**
* Note that there's a bug in the library that doesn't allow us to pass
* the locals to newContext(), hence looping through the keys:
*/

if (options.locals){
for (k in options.locals){
context.setLocals(k, options.locals[k]);
}
delete options.locals;
}
var locals = options.locals || {};

if (options.meta){
context.setLocals('page', options.meta);
locals.pages = options.meta;
delete options.meta;
}

/**
* Add any defined filters:
*/

if (options.filters){
for (k in options.filters){
context.setFilter(k, options.filters[k]);
}

if (options.filters) {
engine.registerFilters(options.filters);
delete options.filters;
}

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

var includeDir = options.includeDir || process.cwd();

context.onInclude(function (name, callback) {
var extname = path.extname(name) ? '' : '.liquid';
var filename = path.resolve(includeDir, name + extname);

fs.readFile(filename, {encoding: 'utf8'}, function (err, data){
if (err) return callback(err);
callback(null, engine.parse(data));
});
});
engine.fileSystem = new Liquid.LocalFileSystem(includeDir, 'liquid');
delete options.includeDir;

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

if (options.customTags){
var tagFunctions = options.customTags;

for (k in options.customTags){
engine.registerTag(k, tagFunctions[k]);
/*Tell jshint there's no problem with having this function in the loop */
/*jshint -W083 */
compileOptions.customTags[k] = function (context, name, body){
var tpl = tagFunctions[name](body.trim());
context.astStack.push(engine.parse(tpl));
};
// compileOptions.customTags[k] = function (context, name, body){
// var tpl = tagFunctions[name](body.trim());
// context.astStack.push(engine.parse(tpl));
// };
/*jshint +W083 */
}
delete options.customTags;
Expand All @@ -268,16 +247,27 @@ exports.liquid.render = function(str, options, fn){
* Now anything left in `options` becomes a local:
*/

for (k in options){
context.setLocals(k, options[k]);
for (var k in options){
locals[k] = options[k];
}

/**
* Finally, execute the template:
*/

return engine
.parseAndRender(str, locals)
.nodeify(function (err, result) {
if (err) {
throw new Error(err);
} else {
return fn(null, result);
}
});

// var tmpl = cache(context) || cache(context, engine.compile(str, compileOptions));
// tmpl(context, fn);

var tmpl = cache(context) || cache(context, engine.compile(str, compileOptions));
tmpl(context, fn);
} catch (err) {
fn(err);
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
"test": "mocha"
},
"dependencies": {
"bluebird": "^3.1.1"
"bluebird": "^3.1.1",
"liquid-node": "^2.6.1",
"slm": "^0.5.0"
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion test/fixtures/liquid/include.liquid
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% include user %}
{% include sub/file %}
{% include "footer.html" %}
{% include "footer.liquid" %}
2 changes: 1 addition & 1 deletion test/shared/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports.test = function(name) {
var str = fs.readFileSync('test/fixtures/' + name + '/filters.' + name).toString();

var locals = { user: user, filters: { toupper: function(object) {
return object.toUpperCase();
return String(object).toUpperCase();
}}};

cons[name].render(str, locals, function(err, html){
Expand Down
3 changes: 1 addition & 2 deletions test/shared/includes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ exports.test = function(name) {

it('should support includes', function(done) {
var str = fs.readFileSync('test/fixtures/' + name + '/include.' + name).toString();

var locals = { user: user, includeDir: 'test/fixtures/' + name };

cons[name].render(str, locals, function(err, html){
if (err) return done(err);
try{
if (name === 'liquid') {
html.should.eql('<p>Tobi</p><section></section><footer></footer>');
html.should.eql('<p>Tobi</p>\n<section></section>\n<footer></footer>');
} else {
html.should.eql('<p>Tobi</p>');
}
Expand Down

0 comments on commit 714f75c

Please sign in to comment.