Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Commit

Permalink
moving setVar to helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
paularmstrong committed Sep 15, 2011
1 parent 89dccd8 commit 32d219e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
11 changes: 11 additions & 0 deletions lib/helpers.js
Expand Up @@ -174,3 +174,14 @@ exports.wrapFilters = function (variable, filters, context, escape) {

return output;
};

exports.setVar = function (varName, argument) {
var out = 'var ' + varName + ';' +
'if (' + check(argument.name) + ') {' +
' ' + varName + ' = ' + exports.wrapFilters(exports.escape(argument.name), argument.filters, null, false) + ';' +
'} else if (' + check(argument.name, '__context') + ') {' +
' ' + varName + ' = ' + exports.wrapFilters(exports.escape(argument.name), argument.filters, '__context', false) + ';' +
'}';

return out;
};
50 changes: 18 additions & 32 deletions lib/tags.js
@@ -1,9 +1,6 @@
var parser = require('./parser'),
helpers = require('./helpers'),
_ = require('underscore'),
check = helpers.check,
escape = helpers.escape,
compile = parser.compile;
_ = require('underscore');

/**
* Inheritance inspired by Django templates
Expand All @@ -14,17 +11,6 @@ exports.extends = {};
exports.block = { ends: true };
exports.parent = {};

function setVar(varName, argument) {
var out = 'var ' + varName + ';' +
'if (' + check(argument.name) + ') {' +
' ' + varName + ' = ' + helpers.wrapFilters(escape(argument.name), argument.filters, null, false) + ';' +
'} else if (' + check(argument.name, '__context') + ') {' +
' ' + varName + ' = ' + helpers.wrapFilters(escape(argument.name), argument.filters, '__context', false) + ';' +
'}';

return out;
}

/**
* Includes another template. The included template will have access to the
* context, but won't have access to the variables defined in the parent template,
Expand All @@ -46,11 +32,11 @@ exports.include = function (indent) {

// Circular includes are VERBOTTEN. This will crash the server.
return ['(function () {'
, ' if (' + check(template) + ') {'
, ' var __template = ' + escape(template) + ';'
, ' if (' + helpers.check(template) + ') {'
, ' var __template = ' + helpers.escape(template) + ';'
, ' }'
, ' else if (' + check(template, '__context') + ') {'
, ' var __template = ' + escape(template, '__context') + ';'
, ' else if (' + helpers.check(template, '__context') + ') {'
, ' var __template = ' + helpers.escape(template, '__context') + ';'
, ' }'
, ' if (typeof __template === "string") {'
, ' __output.push(__this.fromFile(__template).render(__context, __parents));'
Expand Down Expand Up @@ -116,13 +102,13 @@ exports['if'] = function (indent) {
}

out = ['(function () {'];
out.push(setVar('__op1', operand1));
out.push(helpers.setVar('__op1', operand1));
if (operand2.name === '') {
out.push(' if (' + (negation ? '!' : '!!') + '__op1) {');
out.push(compile.call(this, indent + ' '));
out.push(parser.compile.call(this, indent + ' '));
out.push(' }');
} else {
out.push(setVar('__op2', operand2));
out.push(helpers.setVar('__op2', operand2));

if (typeof operator !== 'undefined') {
if (operator === 'in') {
Expand All @@ -131,11 +117,11 @@ exports['if'] = function (indent) {
out.push(' (typeof __op2 === "string" && __op2.indexOf(__op1) > -1) ||');
out.push(' (!Array.isArray(__op2) && typeof __op2 === "object" && __op1 in __op2)');
out.push(' ) {');
out.push(compile.call(this, indent + ' '));
out.push(parser.compile.call(this, indent + ' '));
out.push(' }');
} else {
out.push(' if (__op1 ' + escape(operator) + ' __op2) {');
out.push(compile.call(this, indent + ' '));
out.push(' if (__op1 ' + helpers.escape(operator) + ' __op2) {');
out.push(parser.compile.call(this, indent + ' '));
out.push(' }');
}
}
Expand Down Expand Up @@ -179,26 +165,26 @@ exports['for'] = function (indent) {
}

return ['(function () {'
, ' var ' + escape(operand1) + ','
, ' var ' + helpers.escape(operand1) + ','
, ' forloop = {};'
, setVar('__forloopIter', operand2)
, helpers.setVar('__forloopIter', operand2)
, ' else {'
, ' return;'
, ' }'
, ' if (Array.isArray(__forloopIter)) {'
, ' var __forloopIndex = 0, __forloopLength = __forloopIter.length;'
, ' for (; __forloopIndex < __forloopLength; ++__forloopIndex) {'
, ' forloop.index = __forloopIndex;'
, ' ' + escape(operand1) + ' = __forloopIter[__forloopIndex];'
, compile.call(this, indent + ' ')
, ' ' + helpers.escape(operand1) + ' = __forloopIter[__forloopIndex];'
, parser.compile.call(this, indent + ' ')
, ' }'
, ' } else if (typeof __forloopIter === "object") {'
, ' var __forloopIndex;'
, ' for (__forloopIndex in __forloopIter) {'
, ' if (__forloopIter.hasOwnProperty(__forloopIndex)) {'
, ' forloop.index = __forloopIndex;'
, ' ' + escape(operand1) + ' = __forloopIter[__forloopIndex];'
, compile.call(this, indent + ' ')
, ' ' + helpers.escape(operand1) + ' = __forloopIter[__forloopIndex];'
, parser.compile.call(this, indent + ' ')
, ' }'
, ' }'
, ' }'
Expand All @@ -212,6 +198,6 @@ exports['for'].ends = true;
* Special handling hardcoded into the parser to determine whether variable output should be escaped or not
*/
exports.autoescape = function (indent) {
return compile.call(this, indent);
return parser.compile.call(this, indent);
};
exports.autoescape.ends = true;

0 comments on commit 32d219e

Please sign in to comment.