mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
fix existing tags
new tags: for,cycle
This commit is contained in:
@@ -1,66 +1,52 @@
|
||||
const lexical = require('./lexical.js');
|
||||
const syntax = require('./syntax.js');
|
||||
const error = require('./error.js');
|
||||
const Exp = require('./expression.js');
|
||||
|
||||
function stringify(val) {
|
||||
if (typeof val === 'string') return val;
|
||||
return JSON.stringify(val);
|
||||
}
|
||||
|
||||
function isTruthy(val) {
|
||||
if (val instanceof Array) return !!val.length;
|
||||
return !!val;
|
||||
}
|
||||
|
||||
function factory(Filter, Tag) {
|
||||
function renderTemplates(templates, scope) {
|
||||
var html = '';
|
||||
var template;
|
||||
while (template = templates.shift()) {
|
||||
templates.some(template => {
|
||||
if (scope.get('forloop.skip')) return true;
|
||||
if (template.type === 'tag') {
|
||||
html += template.render(scope);
|
||||
if (template.name === 'continue') {
|
||||
scope.set('forloop.skip', true);
|
||||
return true;
|
||||
}
|
||||
if (template.name === 'break') {
|
||||
scope.set('forloop.stop', true);
|
||||
scope.set('forloop.skip', true);
|
||||
return true;
|
||||
}
|
||||
html += template.render(scope, this.register);
|
||||
} else if (template.type === 'html') {
|
||||
html += template.value;
|
||||
} else if (template.type === 'output') {
|
||||
var val = evalFilter(template.value, scope);
|
||||
html += stringify(val);
|
||||
}
|
||||
}
|
||||
});
|
||||
return html;
|
||||
}
|
||||
|
||||
function evalExp(exp, scope) {
|
||||
if (!scope) error('unable to evalExp: scope undefined');
|
||||
var operatorREs = lexical.operators;
|
||||
for (var i = 0; i < operatorREs.length; i++) {
|
||||
var operatorRE = operatorREs[i];
|
||||
var expRE = new RegExp(`^(${lexical.quoteBalanced.source})(${operatorRE.source})(${lexical.quoteBalanced.source})$`);
|
||||
var match = exp.match(expRE);
|
||||
if (match) {
|
||||
var l = evalExp(match[1], scope);
|
||||
var op = syntax.operators[match[2].trim()];
|
||||
var r = evalExp(match[3], scope);
|
||||
return op(l, r);
|
||||
}
|
||||
}
|
||||
return evalFilter(exp, scope);
|
||||
}
|
||||
|
||||
function evalFilter(str, scope) {
|
||||
if (!scope) error('unable to evalFilter: scope undefined');
|
||||
if (!scope) throw new Error('unable to evalFilter: scope undefined');
|
||||
var filters = str.split('|');
|
||||
var val = scope.get(filters.shift());
|
||||
var val = Exp.evalValue(filters.shift(), scope);
|
||||
return filters
|
||||
.map(str => Filter.construct(str))
|
||||
.reduce((v, filter) => filter.render(v, scope), val);
|
||||
}
|
||||
|
||||
return {
|
||||
renderTemplates, evalFilter, evalExp
|
||||
renderTemplates, evalFilter
|
||||
};
|
||||
};
|
||||
|
||||
factory.isTruthy = isTruthy;
|
||||
factory.stringify = stringify;
|
||||
|
||||
module.exports = factory;
|
||||
|
||||
Reference in New Issue
Block a user