mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-19 14:30:38 -07:00
feature: strict_variables, working on #9
This commit is contained in:
+1
-1
@@ -12,7 +12,7 @@ util.inherits(TokenizationError, Error);
|
||||
|
||||
function ParseError(message, input, line) {
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
this.name = "ParseError";
|
||||
this.name = this.constructor.name;
|
||||
|
||||
this.message = message || "";
|
||||
this.input = input;
|
||||
|
||||
+8
-3
@@ -2,11 +2,16 @@ const error = require('./error.js');
|
||||
const Exp = require('./expression.js');
|
||||
const assert = require('assert');
|
||||
const Promise = require('any-promise');
|
||||
const _ = require('lodash');
|
||||
|
||||
var render = {
|
||||
|
||||
renderTemplates: function(templates, scope) {
|
||||
renderTemplates: function(templates, scope, opts) {
|
||||
assert(scope, 'unable to evalTemplates: scope undefined');
|
||||
opts = _.defaults(opts, {
|
||||
strict_variables: false,
|
||||
strict_filters: false
|
||||
});
|
||||
|
||||
var html = '';
|
||||
|
||||
@@ -15,10 +20,10 @@ var render = {
|
||||
// emptyPromise.then(renderTag(template0).then(renderTag(template1).then(renderTag(template2)...
|
||||
var lastPromise = templates.reduce((promise, template) => {
|
||||
return promise.then((partial) => {
|
||||
if (scope.get('forloop.skip')) {
|
||||
if (scope.safeGet('forloop.skip')) {
|
||||
return Promise.resolve('');
|
||||
}
|
||||
if (scope.get('forloop.stop')) {
|
||||
if (scope.safeGet('forloop.stop')) {
|
||||
throw new Error('forloop.stop'); // this will stop/break the sequential promise chain and go to the catch
|
||||
}
|
||||
|
||||
|
||||
+27
-12
@@ -1,20 +1,30 @@
|
||||
const _ = require('lodash');
|
||||
const lexical = require('./lexical.js');
|
||||
|
||||
var scope = {
|
||||
get: function(str) {
|
||||
var ctx = {};
|
||||
for (var i = this.scopes.length - 1; i >= 0; i--) {
|
||||
if(str === undefined){
|
||||
var Scope = {
|
||||
safeGet: function(str) {
|
||||
var i;
|
||||
// get all
|
||||
if (str === undefined) {
|
||||
var ctx = {};
|
||||
for (i = this.scopes.length - 1; i >= 0; i--) {
|
||||
_.merge(ctx, this.scopes[i]);
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
// get one path
|
||||
for (i = this.scopes.length - 1; i >= 0; i--) {
|
||||
var v = _.get(this.scopes[i], str);
|
||||
if (v !== undefined) return v;
|
||||
if(str === undefined){
|
||||
return ctx;
|
||||
}
|
||||
}
|
||||
},
|
||||
get: function(str){
|
||||
var val = this.safeGet(str);
|
||||
if (val === undefined && this.opts.strict) {
|
||||
throw new Error(`[strict_variables] undefined variable: ${str}`);
|
||||
}
|
||||
return val;
|
||||
},
|
||||
set: function(k, v) {
|
||||
_.set(this.scopes[this.scopes.length - 1], k, v);
|
||||
return this;
|
||||
@@ -28,8 +38,13 @@ var scope = {
|
||||
}
|
||||
};
|
||||
|
||||
exports.factory = function(_ctx) {
|
||||
var ctx = Object.create(scope);
|
||||
ctx.scopes = [_ctx || {}];
|
||||
return ctx;
|
||||
exports.factory = function(_ctx, opts) {
|
||||
opts = _.defaults(opts, {
|
||||
strict: false
|
||||
});
|
||||
|
||||
var scope = Object.create(Scope);
|
||||
scope.opts = opts;
|
||||
scope.scopes = [_ctx || {}];
|
||||
return scope;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user