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

Commit

Permalink
remove deprecated argument for compile/tags
Browse files Browse the repository at this point in the history
  • Loading branch information
paularmstrong committed Oct 20, 2012
1 parent e2cfa13 commit afe9db1
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 29 deletions.
10 changes: 5 additions & 5 deletions docs/custom-tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ First, include the helpers.

Define your tag and whether or not it requires an "end" tag:

exports.mytag = function (indent, parentBlock, parser) {
exports.mytag = function (indent, parser) {
return 'output';
};
exports.mytag.ends = true;
Expand All @@ -26,15 +26,15 @@ A Really Simple Tag <a name="example" href="#example">#</a>

To parse a swig variable with or without filters into a variable token, eg. `bar` or `foo|lowercase`

exports.mytag = function (indent, parentBlock, parser) {
exports.mytag = function (indent, parser) {
var myArg = parser.parseVariable(this.args[0]);
return 'output';
};
exports.mytag.ends = true;

Use a parsed variable token with `helpers.setVar()` to bind a variable in your current scope into the templates scope. The `setVar` method cleans up variable output, applies filters and escaping for clean output:

exports.mytag = function (indent, parentBlock, parser) {
exports.mytag = function (indent, parser) {
var myArg = parser.parseVariable(this.args[0]),
output = '';
output += helpers.setVar(name, myArg);
Expand All @@ -44,7 +44,7 @@ Use a parsed variable token with `helpers.setVar()` to bind a variable in your c

To parse the inner content of a tag for outputting, use `parser.compile.apply(this, [indent, parentBlock])`:

exports.mytag = function (indent, parentBlock, parser) {
exports.mytag = function (indent, parser) {
var myArg = parser.parseVariable(this.args[0]),
output = [];

Expand Down Expand Up @@ -86,7 +86,7 @@ To access a third-party library or method that is defined outside of your templa

Once you've added it, your custom tag can reference the `i18next` extension via the `_ext` object:

exports.trans = function (indent, parentBlock, parser) {
exports.trans = function (indent, parser) {
var myArg = parser.parseVariable(this.args[0]),
output = [];
output.push(helpers.setVar('__myArg', myArg));
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function createTemplate(data, id) {
} else {
render = function (_context, _parents, _filters, _, _ext) {
template.tokens = tokens;
code = parser.compile.call(template, '', null, _context);
code = parser.compile.call(template, '', _context);
var fn = createRenderFunc(code);
return fn.call(this, _context, _parents, _filters, _, _ext);
};
Expand Down
10 changes: 5 additions & 5 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ function precompile(indent, context) {
}
}

exports.compile = function compile(indent, /* option no longer needed */ deprecated, context, template) {
exports.compile = function compile(indent, context, template) {
var code = '',
wrappedInMethod,
blockname,
Expand Down Expand Up @@ -496,25 +496,25 @@ exports.compile = function compile(indent, /* option no longer needed */ depreca
'\" is not in template block list.');
}

code += compile.call(template.blocks[token.args[0]], indent + ' ', null, context, template);
code += compile.call(template.blocks[token.args[0]], indent + ' ', context, template);
} else if (token.name === 'parent') {
parentBlock = getParentBlock(token);
if (!parentBlock) {
throw new Error('No parent block found for parent tag at line ' +
token.line + '.');
}

code += compile.call(parentBlock, indent + ' ', null, context);
code += compile.call(parentBlock, indent + ' ', context);
} else if (token.hasOwnProperty("compile")) {
if (token.strip.start && token.tokens.length && typeof token.tokens[0] === 'string') {
token.tokens[0] = token.tokens[0].replace(/^\s+/, '');
}
if (token.strip.end && token.tokens.length && typeof _.last(token.tokens) === 'string') {
token.tokens[token.tokens.length - 1] = _.last(token.tokens).replace(/\s+$/, '');
}
code += token.compile(indent + ' ', null, exports);
code += token.compile(indent + ' ', exports);
} else {
code += compile.call(token, indent + ' ', null, context);
code += compile.call(token, indent + ' ', context);
}

}, this);
Expand Down
6 changes: 3 additions & 3 deletions lib/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ exports.Tags = testCase({
exports.Whitespace = testCase({
setUp: function (callback) {
var tags = {
foo: function (indent, parentBlock) {
return parser.compile.apply(this, [indent + ' ', parentBlock]);
foo: function (indent) {
return parser.compile.apply(this, [indent + ' ']);
},
bar: function (indent, parentBlock) {
bar: function (indent) {
return '';
}
};
Expand Down
4 changes: 2 additions & 2 deletions lib/tags/autoescape.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* autoescape
* Special handling hardcoded into the parser to determine whether variable output should be escaped or not
*/
module.exports = function (indent, parentBlock, parser) {
return parser.compile.apply(this, [indent, parentBlock]);
module.exports = function (indent, parser) {
return parser.compile.apply(this, [indent]);
};
module.exports.ends = true;
2 changes: 1 addition & 1 deletion lib/tags/else.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var helpers = require('../helpers'),
/**
* else
*/
module.exports = function (indent, parentBlock, parser) {
module.exports = function (indent, parser) {
var last = _.last(this.parent).name,
thisArgs = _.clone(this.args),
ifarg,
Expand Down
4 changes: 2 additions & 2 deletions lib/tags/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ var helpers = require('../helpers'),
/**
* filter
*/
module.exports = function (indent, parentBlock, parser) {
module.exports = function (indent, parser) {
var thisArgs = _.clone(this.args),
name = thisArgs.shift(),
args = (thisArgs.length) ? thisArgs.join(', ') : '',
value = '(function () {\n';
value += ' var _output = "";\n';
value += parser.compile.apply(this, [indent + ' ', parentBlock]) + '\n';
value += parser.compile.apply(this, [indent + ' ']) + '\n';
value += ' return _output;\n';
value += '})()\n';

Expand Down
4 changes: 2 additions & 2 deletions lib/tags/for.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var helpers = require('../helpers'),
/**
* for
*/
module.exports = function (indent, parentBlock, parser) {
module.exports = function (indent, parser) {
var thisArgs = _.clone(this.args),
operand1 = thisArgs[0],
operator = thisArgs[1],
Expand Down Expand Up @@ -35,7 +35,7 @@ module.exports = function (indent, parentBlock, parser) {
'loop.first = (__loopIndex === 0);\n' +
'loop.last = (__loopIndex === __loopLength - 1);\n' +
'_context["' + operand1 + '"] = __loopIter[loop.key];\n' +
parser.compile.apply(this, [indent + ' ', parentBlock]);
parser.compile.apply(this, [indent + ' ']);

out = '(function () {\n' +
' var loop = {}, __loopKey, __loopIndex = 0, __loopLength = 0, __keys = [],' +
Expand Down
4 changes: 2 additions & 2 deletions lib/tags/if.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var helpers = require('../helpers'),
/**
* if
*/
module.exports = function (indent, parentBlock, parser) {
module.exports = function (indent, parser) {
var thisArgs = _.clone(this.args),
args = (helpers.parseIfArgs(thisArgs, parser)),
out = '(function () {\n';
Expand All @@ -20,7 +20,7 @@ module.exports = function (indent, parentBlock, parser) {
out += token.value + ' ';
});
out += ') {\n';
out += parser.compile.apply(this, [indent + ' ', parentBlock]);
out += parser.compile.apply(this, [indent + ' ']);
out += '\n}\n';
out += '})();\n';

Expand Down
2 changes: 1 addition & 1 deletion lib/tags/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var helpers = require('../helpers'),
/**
* import
*/
module.exports = function (indent, parentBlock, parser) {
module.exports = function (indent, parser) {
if (this.args.length !== 3) {
throw new Error('Import statements require three arguments: {% import [template] as [context] %}.');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/tags/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var helpers = require('../helpers'),
/**
* include
*/
module.exports = function (indent, parentBlock, parser) {
module.exports = function (indent, parser) {
var args = _.clone(this.args),
template = args.shift(),
context = '_context',
Expand Down
4 changes: 2 additions & 2 deletions lib/tags/macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var _ = require('underscore');
/**
* macro
*/
module.exports = function (indent, parentBlock, parser) {
module.exports = function (indent, parser) {
var thisArgs = _.clone(this.args),
macro = thisArgs.shift(),
args = '',
Expand All @@ -15,7 +15,7 @@ module.exports = function (indent, parentBlock, parser) {

out += '_context.' + macro + ' = function (' + args + ') {\n';
out += ' var _output = "";\n';
out += parser.compile.apply(this, [indent + ' ', parentBlock]);
out += parser.compile.apply(this, [indent + ' ']);
out += ' return _output;\n';
out += '};\n';

Expand Down
4 changes: 2 additions & 2 deletions lib/tags/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var helpers = require('../helpers'),
/**
* set
*/
module.exports = function (indent, parentBlock, parser) {
module.exports = function (indent, parser) {
var thisArgs = _.clone(this.args),
varname = helpers.escapeVarName(thisArgs.shift(), '_context'),
value;
Expand All @@ -23,7 +23,7 @@ module.exports = function (indent, parentBlock, parser) {
return ' ' + varname + ' = ' +
'(function () {\n' +
' var _output;\n' +
parser.compile.apply({ tokens: [value] }, [indent, parentBlock]) + '\n' +
parser.compile.apply({ tokens: [value] }, [indent]) + '\n' +
' return _output;\n' +
'})();\n';
};

0 comments on commit afe9db1

Please sign in to comment.