Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'apichange' into dev

  • Loading branch information...
commit 156de6f3abfa141bba460938f3d36cb9cbd9c22a 2 parents 60a227d + 4c3c554
@paularmstrong authored
View
2  README.md
@@ -42,7 +42,7 @@ Basic Example
### node.js code
var template = require('swig');
- var tmpl = template.fromFile('/path/to/template.html');
+ var tmpl = template.compileFile('/path/to/template.html');
tmpl.render({
pagename: 'awesome people',
authors: ['Paul', 'Jim', 'Jane']
View
8 docs/browser.md
@@ -16,18 +16,18 @@ You should only need one of these. If you already include _underscore.js_ in you
## The API <a name="api" href="#api">#</a>
-Swig's API in the browser is the same as [its API for node.js](getting-started.md), with the only difference being that you cannot use the `swig.fromFile` method, since browsers do not have a filesystem.
+Swig's API in the browser is the same as [its API for node.js](getting-started.md), with the only difference being that you cannot use the `swig.compileFile` method, since browsers do not have a filesystem.
Instead, you must always use `swig.fromString` and pre-parse all templates.
In order to use `extends`, `import`, and `include` correctly, another argument is available on `swig.fromString`: `templateKey`
- swig.fromString(templateString, templateKey);
+ swig.compile(templateString, { filename: templateKey });
This is the key that will be used to lookup a template. For instance:
- var template = swig.fromString('<p>{% block content %}{% endblock %}</p>', 'main');
- var mypage = swig.fromString('{% extends "main" %}{% block content %}Oh hey there!{% endblock %}');
+ var template = swig.compile('<p>{% block content %}{% endblock %}</p>', { filename: 'main' });
+ var mypage = swig.compile('{% extends "main" %}{% block content %}Oh hey there!{% endblock %}', { filename: 'mypage' });
When you render mypage, `mypage.render({});`, you will see
View
13 docs/getting-started.md
@@ -45,7 +45,7 @@ Use this to set any custom filters and/or override any of the built-in filters.
#### root _optional_
-The directory to search for templates. If a template passed to `swig.fromFile` is an absolute path (starts with `/`), Swig will not look in the template root.
+The directory to search for templates. If a template passed to `swig.compileFile` is an absolute path (starts with `/`), Swig will not look in the template root.
#### tags _optional_
@@ -56,13 +56,18 @@ Parsing a Template <a name="parsing" href="#parsing">#</a>
You have 2 methods for creating a template object:
- swig.fromFile("path/to/template/file.html");
- swig.fromString("Template string here");
+ swig.compileFile("path/to/template/file.html");
+ swig.compile("Template string here", { filename: 'templateKey' });
Rendering a Template <a name="rendering" href="#rendering">#</a>
--------------------
Both of these methods will give you a template object on which you call the render method passing it a map of context values.
- var tpl = swig.fromFile("path/to/template/file.html");
+ var tpl = swig.compileFile("path/to/template/file.html");
var renderedHtml = tpl.render({ vars: 'to be inserted in template' });
+
+OR
+
+ var tpl = swig.compile("path/to/template/file.html");
+ var renderedHtml = tpl({ vars: 'to be inserted in template' });
View
2  examples/basic/server.js
@@ -6,7 +6,7 @@ swig.init({
});
http.createServer(function (req, res) {
- var tmpl = swig.fromFile('page.html'),
+ var tmpl = swig.compileFile('page.html'),
renderedHtml = tmpl.render({
people: [
{ name: 'Paul', age: 28 },
View
2  examples/custom_tags/server.js
@@ -7,7 +7,7 @@ swig.init({
});
http.createServer(function (req, res) {
- var tmpl = swig.fromFile('page.html'),
+ var tmpl = swig.compileFile('page.html'),
renderedHtml = tmpl.render({
people: [
{ id: 'person0', name: 'Paul', age: 28 },
View
3  examples/express/server.js
@@ -12,7 +12,8 @@ swig.init({ root: __dirname + '/views', allowErrors: true });
app.set('views', __dirname + '/views');
// Make sure you aren't using Express's built-in layout extending
-app.set('view options', { layout: false });
+// app.set('view options', { layout: false });
+app.set('view cache', true);
app.get('/', function (req, res) {
res.render('index', {});
View
2  examples/macros/server.js
@@ -6,7 +6,7 @@ swig.init({
});
http.createServer(function (req, res) {
- var tmpl = swig.fromFile('index.html'),
+ var tmpl = swig.compileFile('index.html'),
renderedHtml = tmpl.render({
people: [
{ name: 'Paul', age: 28 },
View
66 index.js
@@ -6,11 +6,12 @@ var fs = require('fs'),
filters = require('./lib/filters'),
helpers = require('./lib/helpers'),
- _ = require('underscore');
+ _ = require('underscore'),
-var config = {
+ config = {
allowErrors: false,
autoescape: true,
+ cache: true,
encoding: 'utf8',
filters: filters,
root: '/',
@@ -21,6 +22,7 @@ var config = {
// Call this before using the templates
exports.init = function (options) {
+ CACHE = {};
_config = _.extend({}, config, options);
_config.filters = _.extend(filters, options.filters);
_config.tags = _.extend(tags, options.tags);
@@ -35,7 +37,7 @@ function TemplateError(error) {
function createTemplate(data, id) {
var template = {
// Allows us to include templates from the compiled code
- fromFile: exports.fromFile,
+ compileFile: exports.compileFile,
// These are the blocks inside the template
blocks: {},
// Distinguish from other tokens
@@ -95,23 +97,38 @@ function createTemplate(data, id) {
return template;
}
-exports.fromFile = function (filepath) {
+function getTemplate(source, options) {
+ var key = options.filename || source;
+ if (_config.cache || options.cache) {
+ if (!CACHE.hasOwnProperty(key)) {
+ CACHE[key] = createTemplate(source, key);
+ }
+
+ return CACHE[key];
+ }
+
+ return createTemplate(source, key);
+}
+
+exports.compileFile = function (filepath) {
+ var tpl, get;
+
if (filepath[0] === '/') {
filepath = filepath.substr(1);
}
- if (CACHE.hasOwnProperty(filepath)) {
+ if (_config.cache && CACHE.hasOwnProperty(filepath)) {
return CACHE[filepath];
}
if (typeof window !== 'undefined') {
- throw new TemplateError({ stack: 'You must pre-compile all templates in-browser. Use `swig.fromString(template);`.' });
+ throw new TemplateError({ stack: 'You must pre-compile all templates in-browser. Use `swig.compile(template);`.' });
}
- var get = function () {
+ get = function () {
var file = ((/^\//).test(filepath)) ? filepath : _config.root + '/' + filepath,
data = fs.readFileSync(file, config.encoding);
- CACHE[filepath] = createTemplate(data, filepath);
+ tpl = getTemplate(data, { filename: filepath });
};
if (_config.allowErrors) {
@@ -120,34 +137,17 @@ exports.fromFile = function (filepath) {
try {
get();
} catch (error) {
- CACHE[filepath] = new TemplateError(error);
+ tpl = new TemplateError(error);
}
}
- return CACHE[filepath];
+ return tpl;
};
-exports.fromString = function (string, name) {
- var key = name || string;
- if (!CACHE.hasOwnProperty(key)) {
- CACHE[key] = createTemplate(string, key);
- }
-
- return CACHE[key];
-};
-
-exports.compile = function (source, options, callback) {
- var self = this;
- if (typeof source === 'string') {
- return function (options) {
- var tmpl = exports.fromString(source);
- return tmpl.render(options);
- };
- } else {
- return source;
- }
-};
+exports.compile = function (source, options) {
+ options = options || {};
+ var tmpl = getTemplate(source, options || {});
-exports.render = function (template, options) {
- template = exports.compile(template, options);
- return template(options);
+ return function (source, options) {
+ return tmpl.render(source, options);
+ };
};
View
2  lib/parser.js
@@ -264,7 +264,7 @@ exports.compile = function compile(indent, parentBlock) {
if (index > 0) {
throw new Error('Extends tag must be the first tag in the template, but "extends" found on line ' + token.line + '.');
}
- token.template = this.fromFile(filepath.replace(/['"]/g, ''));
+ token.template = this.compileFile(filepath.replace(/['"]/g, ''));
this.parent = token.template;
} else if (token.name === 'block') { // Make a list of blocks
blockname = token.args[0];
View
4 lib/tags.js
@@ -36,7 +36,7 @@ exports.include = function (indent) {
'(function () {',
helpers.setVar('__template', parser.parseVariable(template)),
' if (typeof __template === "string") {',
- ' __output += __this.fromFile(__template).render(__context, __parents);',
+ ' __output += __this.compileFile(__template).render(__context, __parents);',
' }',
'})();'
].join('\n' + indent);
@@ -305,7 +305,7 @@ exports['import'] = function (indent) {
out += 'var __context = {}, __ctx = {}, __output = "";\n' +
helpers.setVar('__template', parser.parseVariable(file)) +
- '__this.fromFile(__template).render(__ctx, __parents);\n' +
+ '__this.compileFile(__template).render(__ctx, __parents);\n' +
'_.each(__ctx, function (item, key) {\n' +
' if (typeof item === "function") {\n' +
' __context["' + name + '_" + key] = item;\n' +
View
2  package.json
@@ -15,7 +15,7 @@
"underscore": ">=1.1.7"
},
"devDependencies": {
- "express": ">=2.4.0",
+ "express": ">=2.5.0",
"nodelint": ">=0.5.1",
"nodeunit": ">=0.5.5",
"uglify-js": ">=1.1.0"
View
6 tests/filters.test.js
@@ -3,8 +3,8 @@ var swig = require('../index');
swig.init({});
function testFilter(test, filter, input, output, message) {
- var tpl = swig.fromString('{{ v|' + filter + ' }}');
- test.strictEqual(tpl.render(input), output, message);
+ var tpl = swig.compile('{{ v|' + filter + ' }}');
+ test.strictEqual(tpl(input), output, message);
}
exports.add = function (test) {
@@ -34,7 +34,7 @@ exports.capitalize = function (test) {
exports.date = function (test) {
var date = new Date(2011, 8, 6, 9, 5, 2),
- tpl = swig.fromString('{{ d|date("d") }}');
+ tpl = swig.compile('{{ d|date("d") }}');
function testFormat(format, expected) {
testFilter(test, 'date("' + format + '")', { v: date }, expected);
View
14 tests/index.test.js
@@ -1,7 +1,7 @@
var swig = require('../index'),
testCase = require('nodeunit').testCase;
-exports.fromFile = testCase({
+exports.compileFile = testCase({
setUp: function (callback) {
callback();
},
@@ -12,7 +12,7 @@ exports.fromFile = testCase({
allowErrors: true
});
- var tpl = swig.fromFile('included_2.html');
+ var tpl = swig.compileFile('included_2.html');
test.strictEqual('2', tpl.render({ array: [1, 1] }), 'from file is a-ok');
test.done();
},
@@ -22,9 +22,9 @@ exports.fromFile = testCase({
root: __dirname + '/templates',
allowErrors: false
});
- var tpl = swig.fromFile('foobar.html');
+ var tpl = swig.compileFile('foobar.html');
test.ok((/<pre>Error\: ENOENT, No such file or directory/).test(tpl.render()), 'pushes a render function with the error');
- tpl = swig.fromFile('includes_notfound.html');
+ tpl = swig.compileFile('includes_notfound.html');
test.ok((/<pre>Error\: ENOENT, No such file or directory/).test(tpl.render()), 'renders the error when includes a file that is not found');
test.done();
@@ -33,7 +33,7 @@ exports.fromFile = testCase({
'allowErrors = true': function (test) {
swig.init({ allowErrors: true });
test.throws(function () {
- swig.fromFile('barfoo.html');
+ swig.compileFile('barfoo.html');
}, 'throws when allowErrors is true');
test.done();
@@ -44,7 +44,7 @@ exports.fromFile = testCase({
root: __dirname + '/foobar',
allowErrors: true
});
- var tpl = swig.fromFile('/' + __dirname + '/templates/included_2.html');
+ var tpl = swig.compileFile('/' + __dirname + '/templates/included_2.html');
test.strictEqual('2', tpl.render({ array: [1, 1] }), 'file from absolute path is a-ok');
test.done();
},
@@ -53,7 +53,7 @@ exports.fromFile = testCase({
swig.init({});
global.window = true;
test.throws(function () {
- swig.fromFile('foobar');
+ swig.compileFile('foobar');
});
delete global.window;
test.done();
View
24 tests/speed.js
@@ -1,4 +1,4 @@
-var template = require('../index'),
+var swig = require('../index'),
tplF,
tplS,
array,
@@ -10,18 +10,18 @@ var template = require('../index'),
console.log();
console.log('Starting speed tests...');
-template.init({
+swig.init({
root: __dirname + '/templates'
});
tplString = '{% for foo in bar %}{{ forloop.index }} - {{ foo }}{% endfor %}';
-tplF = template.fromString(tplString);
+tplF = swig.compile(tplString);
console.time('object loop');
i = 10000;
d = new Date();
while (i) {
i -= 1;
- tplF.render({bar: { foo: 'bar', bar: 'baz', baz: 'bop' }});
+ tplF({bar: { foo: 'bar', bar: 'baz', baz: 'bop' }});
}
console.timeEnd('object loop');
console.log(" ~ " + Math.round(1000000 / (new Date() - d)) + " renders per sec.");
@@ -31,7 +31,7 @@ i = 10000;
d = new Date();
while (i) {
i -= 1;
- tplF.render({ bar: [ 'bar', 'baz', 'bop' ]});
+ tplF({ bar: [ 'bar', 'baz', 'bop' ]});
}
console.timeEnd('array loop');
console.log(" ~ " + Math.round(1000000 / (new Date() - d)) + " renders per sec.");
@@ -53,15 +53,15 @@ console.time('Compile one template 1000 times');
d = new Date();
while (i) {
i -= 1;
- tplS = template.fromString(tplString);
+ tplS = swig.compile(tplString);
}
console.timeEnd('Compile one template 1000 times');
console.log(" ~ " + Math.round(1000000 / (new Date() - d)) + " renders per sec.");
array = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], { af: "s", baz: "d", d: "f" }, "zeus"];
-tplF = template.fromFile("include_base.html");
-template.fromFile("included_2.html");
-template.fromFile("included.html");
+tplF = swig.compileFile("include_base.html");
+swig.compileFile("included_2.html");
+swig.compileFile("included.html");
i = 1000;
console.time('Render 1000 Includes Templates');
@@ -73,9 +73,9 @@ while (i) {
console.timeEnd('Render 1000 Includes Templates');
console.log(" ~ " + Math.round(1000000 / (new Date() - d)) + " renders per sec.");
-template.fromFile("extends_base.html");
-template.fromFile("extends_1.html");
-tplF = template.fromFile("extends_2.html");
+swig.compileFile("extends_base.html");
+swig.compileFile("extends_1.html");
+tplF = swig.compileFile("extends_2.html");
i = 1000;
console.time('Render 1000 Extends Templates');
View
245 tests/tags.test.js
@@ -8,14 +8,14 @@ exports['custom tags'] = function (test) {
return '__output += "hi!";';
}
},
- tmpl8;
+ tpl;
tags.foo.ends = true;
swig.init({ tags: tags });
- tmpl8 = swig.fromString('{% foo %}{% endfoo %}');
- test.strictEqual(tmpl8.render({}), 'hi!');
+ tpl = swig.compile('{% foo %}{% endfoo %}');
+ test.strictEqual(tpl({}), 'hi!');
test.done();
};
@@ -26,24 +26,24 @@ exports.autoescape = testCase({
},
on: function (test) {
- var tpl = swig.fromString('{% autoescape on %}{{ foo }}{% endautoescape %}');
- test.strictEqual(tpl.render({ foo: '<\'single\' & "double" quotes>' }), '&lt;&#39;single&#39; &amp; &quot;double&quot; quotes&gt;');
+ var tpl = swig.compile('{% autoescape on %}{{ foo }}{% endautoescape %}');
+ test.strictEqual(tpl({ foo: '<\'single\' & "double" quotes>' }), '&lt;&#39;single&#39; &amp; &quot;double&quot; quotes&gt;');
test.done();
},
off: function (test) {
- var tpl = swig.fromString('{% autoescape off %}{{ foo }}{% endautoescape %}');
- test.strictEqual(tpl.render({ foo: '<\'single\' & "double" quotes>' }), '<\'single\' & "double" quotes>');
+ var tpl = swig.compile('{% autoescape off %}{{ foo }}{% endautoescape %}');
+ test.strictEqual(tpl({ foo: '<\'single\' & "double" quotes>' }), '<\'single\' & "double" quotes>');
test.done();
},
js: function (test) {
- var tpl = swig.fromString('{% autoescape on "js" %}{{ foo }}{% endautoescape %}');
- test.strictEqual(tpl.render({ foo: '"double quotes" and \'single quotes\'' }), '\\u0022double quotes\\u0022 and \\u0027single quotes\\u0027');
- test.strictEqual(tpl.render({ foo: '<script>and this</script>' }), '\\u003Cscript\\u003Eand this\\u003C/script\\u003E');
- test.strictEqual(tpl.render({ foo: '\\ : backslashes, too' }), '\\u005C : backslashes, too');
- test.strictEqual(tpl.render({ foo: 'and lots of whitespace: \r\n\t\v\f\b' }), 'and lots of whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008');
- test.strictEqual(tpl.render({ foo: 'and "special" chars = -1;' }), 'and \\u0022special\\u0022 chars \\u003D \\u002D1\\u003B');
+ var tpl = swig.compile('{% autoescape on "js" %}{{ foo }}{% endautoescape %}');
+ test.strictEqual(tpl({ foo: '"double quotes" and \'single quotes\'' }), '\\u0022double quotes\\u0022 and \\u0027single quotes\\u0027');
+ test.strictEqual(tpl({ foo: '<script>and this</script>' }), '\\u003Cscript\\u003Eand this\\u003C/script\\u003E');
+ test.strictEqual(tpl({ foo: '\\ : backslashes, too' }), '\\u005C : backslashes, too');
+ test.strictEqual(tpl({ foo: 'and lots of whitespace: \r\n\t\v\f\b' }), 'and lots of whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008');
+ test.strictEqual(tpl({ foo: 'and "special" chars = -1;' }), 'and \\u0022special\\u0022 chars \\u003D \\u002D1\\u003B');
test.done();
}
});
@@ -76,29 +76,30 @@ exports['extends'] = testCase({
},
basic: function (test) {
- var tmpl8;
+ var tpl;
if (typeof window !== 'undefined') {
- swig.fromString(this.extends_base, 'extends_base.html');
- tmpl8 = swig.fromString(this.extends1, 'extends1.html');
+ swig.compile(this.extends_base, { filename: 'extends_base.html' });
+ tpl = swig.compile(this.extends1, { filename: 'extends1.html' });
+ test.strictEqual('This is from the "extends_base.html" template.\n\n\n This is the "extends_1.html" content in block \'one\'\n\n\n This is the default content in block \'two\'\n', tpl({}));
} else {
- tmpl8 = swig.fromFile('extends_1.html');
+ tpl = swig.compileFile('extends_1.html');
+ test.strictEqual('This is from the "extends_base.html" template.\n\n\n This is the "extends_1.html" content in block \'one\'\n\n\n This is the default content in block \'two\'\n', tpl.render({}));
}
- test.strictEqual('This is from the "extends_base.html" template.\n\n\n This is the "extends_1.html" content in block \'one\'\n\n\n This is the default content in block \'two\'\n', tmpl8.render());
test.done();
},
circular: function (test) {
- var tmpl8;
+ var tpl;
if (typeof window !== 'undefined') {
swig.init({ allowErrors: true });
test.throws(function () {
- swig.fromString(this.circular1, 'extends_circular1.html');
- tmpl8 = swig.fromString(this.circular2, 'extends_circular2.html');
- tmpl8.render();
+ swig.compile(this.circular1, { filename: 'extends_circular1.html' });
+ tpl = swig.compile(this.circular2, { filename: 'extends_circular2.html' });
+ tpl();
});
} else {
- tmpl8 = swig.fromFile('extends_circular1.html');
- test.ok((/^<pre>Error: Circular extends found on line 3 of \"extends_circular1\.html\"\!/).test(tmpl8.render()), 'throws an error');
+ tpl = swig.compileFile('extends_circular1.html');
+ test.ok((/^<pre>Error: Circular extends found on line 3 of \"extends_circular1\.html\"\!/).test(tpl.render({})), 'throws an error');
}
test.done();
}
@@ -112,19 +113,19 @@ exports.include = testCase({
basic: function (test) {
if (typeof window !== 'undefined') {
- swig.fromString('{{array.length}}', 'included_2.html');
+ swig.compile('{{array.length}}', { filename: 'included_2.html' });
}
- var tmpl8 = swig.fromString('{% include "included_2.html" %}');
- test.strictEqual(tmpl8.render({ array: ['foo'] }), '1');
+ var tpl = swig.compile('{% include "included_2.html" %}');
+ test.strictEqual(tpl({ array: ['foo'] }), '1');
test.done();
},
variable: function (test) {
if (typeof window !== 'undefined') {
- swig.fromString('{{array.length}}', 'included_2.html');
+ swig.compile('{{array.length}}', { filename: 'included_2.html' });
}
- var tmpl8 = swig.fromString('{% include inc %}');
- test.strictEqual(tmpl8.render({ inc: 'included_2.html', array: ['foo'] }), '1');
+ var tpl = swig.compile('{% include inc %}');
+ test.strictEqual(tpl({ inc: 'included_2.html', array: ['foo'] }), '1');
test.done();
}
});
@@ -136,86 +137,86 @@ exports['if'] = testCase({
},
basic: function (test) {
- var tpl = swig.fromString('{% if foo %}hi!{% endif %}{% if bar %}nope{% endif %}');
- test.strictEqual(tpl.render({ foo: 1, bar: false }), 'hi!');
+ var tpl = swig.compile('{% if foo %}hi!{% endif %}{% if bar %}nope{% endif %}');
+ test.strictEqual(tpl({ foo: 1, bar: false }), 'hi!');
- tpl = swig.fromString('{% if !foo %}hi!{% endif %}{% if !bar %}nope{% endif %}');
- test.strictEqual(tpl.render({ foo: 1, bar: false }), 'nope', '! operator');
+ tpl = swig.compile('{% if !foo %}hi!{% endif %}{% if !bar %}nope{% endif %}');
+ test.strictEqual(tpl({ foo: 1, bar: false }), 'nope', '! operator');
- tpl = swig.fromString('{% if not foo %}hi!{% endif %}{% if not bar %}nope{% endif %}');
- test.strictEqual(tpl.render({ foo: true, bar: false }), 'nope', 'not operator');
+ tpl = swig.compile('{% if not foo %}hi!{% endif %}{% if not bar %}nope{% endif %}');
+ test.strictEqual(tpl({ foo: true, bar: false }), 'nope', 'not operator');
- tpl = swig.fromString('{% if foo && (bar || baz) %}hi!{% endif %}');
- test.strictEqual(tpl.render({ foo: true, bar: true }), 'hi!');
- test.strictEqual(tpl.render({ foo: true, baz: true }), 'hi!');
- test.strictEqual(tpl.render({ foo: false }), '');
+ tpl = swig.compile('{% if foo && (bar || baz) %}hi!{% endif %}');
+ test.strictEqual(tpl({ foo: true, bar: true }), 'hi!');
+ test.strictEqual(tpl({ foo: true, baz: true }), 'hi!');
+ test.strictEqual(tpl({ foo: false }), '');
- tpl = swig.fromString('{% if foo and bar %}hi!{% endif %}');
- test.strictEqual(tpl.render({ foo: true, bar: true }), 'hi!', 'and syntax');
- tpl = swig.fromString('{% if foo or bar %}hi!{% endif %}');
- test.strictEqual(tpl.render({ foo: false, bar: true }), 'hi!', 'or syntax');
+ tpl = swig.compile('{% if foo and bar %}hi!{% endif %}');
+ test.strictEqual(tpl({ foo: true, bar: true }), 'hi!', 'and syntax');
+ tpl = swig.compile('{% if foo or bar %}hi!{% endif %}');
+ test.strictEqual(tpl({ foo: false, bar: true }), 'hi!', 'or syntax');
- tpl = swig.fromString('{% if foo in bar %}hi!{% endif %}');
- test.strictEqual(tpl.render({ foo: 'b', bar: ['a', 'b', 'c'] }), 'hi!', 'in syntax');
+ tpl = swig.compile('{% if foo in bar %}hi!{% endif %}');
+ test.strictEqual(tpl({ foo: 'b', bar: ['a', 'b', 'c'] }), 'hi!', 'in syntax');
test.done();
},
errors: function (test) {
test.throws(function () {
- swig.fromString('{% if foo bar %}{% endif %}');
+ swig.compile('{% if foo bar %}{% endif %}');
});
test.throws(function () {
- swig.fromString('{% if foo !== > bar %}{% endif %}');
+ swig.compile('{% if foo !== > bar %}{% endif %}');
});
test.throws(function () {
- swig.fromString('{% if (foo %}{% endif %}');
+ swig.compile('{% if (foo %}{% endif %}');
});
test.throws(function () {
- swig.fromString('{% if foo > bar) %}{% endif %}');
+ swig.compile('{% if foo > bar) %}{% endif %}');
});
test.done();
},
'var literals in tags allow filters': function (test) {
- var tmpl8 = swig.fromString('{% if foo|length > 1 %}hi!{% endif %}');
- test.strictEqual(tmpl8.render({ foo: [1, 2, 3] }), 'hi!');
+ var tpl = swig.compile('{% if foo|length > 1 %}hi!{% endif %}');
+ test.strictEqual(tpl({ foo: [1, 2, 3] }), 'hi!');
- tmpl8 = swig.fromString('{% if foo|length === bar|length %}hi!{% endif %}{% if foo|length !== bar|length %}fail{% endif %}');
- test.strictEqual(tmpl8.render({ foo: [1, 2], bar: [3, 4] }), 'hi!');
+ tpl = swig.compile('{% if foo|length === bar|length %}hi!{% endif %}{% if foo|length !== bar|length %}fail{% endif %}');
+ test.strictEqual(tpl({ foo: [1, 2], bar: [3, 4] }), 'hi!');
test.done();
},
'else': function (test) {
- var tmpl8 = swig.fromString('{% if foo|length > 1 %}hi!{% else %}nope{% endif %}');
- test.strictEqual(tmpl8.render({ foo: [1, 2, 3] }), 'hi!');
- test.strictEqual(tmpl8.render({ foo: [1] }), 'nope');
+ var tpl = swig.compile('{% if foo|length > 1 %}hi!{% else %}nope{% endif %}');
+ test.strictEqual(tpl({ foo: [1, 2, 3] }), 'hi!');
+ test.strictEqual(tpl({ foo: [1] }), 'nope');
test.throws(function () {
- swig.fromString('{% for i in foo %}hi!{% else %}nope{% endfor %}');
+ swig.compile('{% for i in foo %}hi!{% else %}nope{% endfor %}');
}, Error, 'Cannot call else tag outside of "if" context.');
test.done();
},
'else if': function (test) {
- var tmpl8 = swig.fromString('{% if foo|length > 2 %}foo{% else if foo|length < 2 %}bar{% endif %}');
- test.strictEqual(tmpl8.render({ foo: [1, 2, 3] }), 'foo');
- test.strictEqual(tmpl8.render({ foo: [1, 2] }), '');
- test.strictEqual(tmpl8.render({ foo: [1] }), 'bar');
+ var tpl = swig.compile('{% if foo|length > 2 %}foo{% else if foo|length < 2 %}bar{% endif %}');
+ test.strictEqual(tpl({ foo: [1, 2, 3] }), 'foo');
+ test.strictEqual(tpl({ foo: [1, 2] }), '');
+ test.strictEqual(tpl({ foo: [1] }), 'bar');
- tmpl8 = swig.fromString('{% if foo %}foo{% else if bar && baz %}bar{% endif %}');
- test.strictEqual(tmpl8.render({ bar: true, baz: true }), 'bar');
+ tpl = swig.compile('{% if foo %}foo{% else if bar && baz %}bar{% endif %}');
+ test.strictEqual(tpl({ bar: true, baz: true }), 'bar');
test.done();
},
'multiple else if and else': function (test) {
- var tmpl8 = swig.fromString('{% if foo %}foo{% else if bar === "bar" %}bar{% else if 3 in baz %}baz{% else %}bop{% endif %}');
- test.strictEqual(tmpl8.render({ foo: true }), 'foo');
- test.strictEqual(tmpl8.render({ bar: "bar" }), 'bar');
- test.strictEqual(tmpl8.render({ baz: [3] }), 'baz');
- test.strictEqual(tmpl8.render({ baz: [2] }), 'bop');
- test.strictEqual(tmpl8.render({ bar: false }), 'bop');
+ var tpl = swig.compile('{% if foo %}foo{% else if bar === "bar" %}bar{% else if 3 in baz %}baz{% else %}bop{% endif %}');
+ test.strictEqual(tpl({ foo: true }), 'foo');
+ test.strictEqual(tpl({ bar: "bar" }), 'bar');
+ test.strictEqual(tpl({ baz: [3] }), 'baz');
+ test.strictEqual(tpl({ baz: [2] }), 'bop');
+ test.strictEqual(tpl({ bar: false }), 'bop');
test.done();
}
@@ -228,57 +229,57 @@ exports['for'] = testCase({
},
basic: function (test) {
- var tmpl8 = swig.fromString('{% for foo in bar %}{{ foo }}, {% endfor %}');
- test.strictEqual(tmpl8.render({ bar: ['foo', 'bar', 'baz'] }), 'foo, bar, baz, ', 'array loop');
- test.strictEqual(tmpl8.render({ bar: { baz: 'foo', pow: 'bar', foo: 'baz' }}), 'foo, bar, baz, ', 'object loop');
+ var tpl = swig.compile('{% for foo in bar %}{{ foo }}, {% endfor %}');
+ test.strictEqual(tpl({ bar: ['foo', 'bar', 'baz'] }), 'foo, bar, baz, ', 'array loop');
+ test.strictEqual(tpl({ bar: { baz: 'foo', pow: 'bar', foo: 'baz' }}), 'foo, bar, baz, ', 'object loop');
test.done();
},
variables: function (test) {
- var tmpl8 = swig.fromString('{% for foo in bar %}[{{ forloop.index }}, {{ forloop.key }}]{% endfor %}');
- test.strictEqual(tmpl8.render({ bar: ['foo', 'bar', 'baz'] }), '[0, 0][1, 1][2, 2]', 'array loop');
- test.strictEqual(tmpl8.render({ bar: { baz: 'foo', pow: 'bar', foo: 'baz' }}), '[0, baz][1, pow][2, foo]', 'object loop');
+ var tpl = swig.compile('{% for foo in bar %}[{{ forloop.index }}, {{ forloop.key }}]{% endfor %}');
+ test.strictEqual(tpl({ bar: ['foo', 'bar', 'baz'] }), '[0, 0][1, 1][2, 2]', 'array loop');
+ test.strictEqual(tpl({ bar: { baz: 'foo', pow: 'bar', foo: 'baz' }}), '[0, baz][1, pow][2, foo]', 'object loop');
test.done();
},
index: function (test) {
- var tmpl8 = swig.fromString('{% for foo in bar %}{{ forloop.index }}{% endfor %}');
- test.strictEqual(tmpl8.render({ bar: ['foo', 'bar', 'baz'] }), '012', 'index in object');
- test.strictEqual(tmpl8.render({ bar: { baz: 'foo', pow: 'bar', foo: 'baz' }}), '012', 'index in object');
+ var tpl = swig.compile('{% for foo in bar %}{{ forloop.index }}{% endfor %}');
+ test.strictEqual(tpl({ bar: ['foo', 'bar', 'baz'] }), '012', 'index in object');
+ test.strictEqual(tpl({ bar: { baz: 'foo', pow: 'bar', foo: 'baz' }}), '012', 'index in object');
test.done();
},
first: function (test) {
- var tmpl8 = swig.fromString('{% for foo in bar %}{% if forloop.first %}{{ foo }}{% endif %}{% endfor %}');
- test.strictEqual(tmpl8.render({ bar: ['foo', 'bar', 'baz'] }), 'foo', 'first in array');
- test.strictEqual(tmpl8.render({ bar: { baz: 'foo', pow: 'bar', foo: 'baz' }}), 'foo', 'first in object');
+ var tpl = swig.compile('{% for foo in bar %}{% if forloop.first %}{{ foo }}{% endif %}{% endfor %}');
+ test.strictEqual(tpl({ bar: ['foo', 'bar', 'baz'] }), 'foo', 'first in array');
+ test.strictEqual(tpl({ bar: { baz: 'foo', pow: 'bar', foo: 'baz' }}), 'foo', 'first in object');
test.done();
},
last: function (test) {
- var tmpl8 = swig.fromString('{% for foo in bar %}{% if forloop.last %}{{ foo }}{% endif %}{% endfor %}');
- test.strictEqual(tmpl8.render({ bar: ['foo', 'bar', 'baz'] }), 'baz', 'last in array');
- test.strictEqual(tmpl8.render({ bar: { baz: 'foo', pow: 'bar', foo: 'baz' }}), 'baz', 'last in object');
+ var tpl = swig.compile('{% for foo in bar %}{% if forloop.last %}{{ foo }}{% endif %}{% endfor %}');
+ test.strictEqual(tpl({ bar: ['foo', 'bar', 'baz'] }), 'baz', 'last in array');
+ test.strictEqual(tpl({ bar: { baz: 'foo', pow: 'bar', foo: 'baz' }}), 'baz', 'last in object');
test.done();
},
empty: function (test) {
- var tmpl8 = swig.fromString('{% for foo in bar %}blah{% empty %}hooray!{% endfor %}');
- test.strictEqual(tmpl8.render({ bar: [] }), 'hooray!', 'empty in array');
- test.strictEqual(tmpl8.render({ bar: {}}), 'hooray!', 'empty in object');
+ var tpl = swig.compile('{% for foo in bar %}blah{% empty %}hooray!{% endfor %}');
+ test.strictEqual(tpl({ bar: [] }), 'hooray!', 'empty in array');
+ test.strictEqual(tpl({ bar: {}}), 'hooray!', 'empty in object');
- test.strictEqual(tmpl8.render({ bar: [1] }), 'blah', 'not empty in array');
- test.strictEqual(tmpl8.render({ bar: { foo: 'foo' }}), 'blah', 'not empty in object');
+ test.strictEqual(tpl({ bar: [1] }), 'blah', 'not empty in array');
+ test.strictEqual(tpl({ bar: { foo: 'foo' }}), 'blah', 'not empty in object');
test.throws(function () {
- swig.fromString('{% if foo %}hi!{% empty %}nope{% endif %}');
+ swig.compile('{% if foo %}hi!{% empty %}nope{% endif %}');
}, Error, 'Cannot call "empty" tag outside of "for" context.');
test.done();
},
'loop object allows filters': function (test) {
- var tmpl8 = swig.fromString('{% for foo in bar|reverse %}{{ foo }}{% endfor %}');
- test.strictEqual(tmpl8.render({ bar: ['baz', 'bar', 'foo'] }), 'foobarbaz');
+ var tpl = swig.compile('{% for foo in bar|reverse %}{{ foo }}{% endfor %}');
+ test.strictEqual(tpl({ bar: ['baz', 'bar', 'foo'] }), 'foobarbaz');
test.done();
}
});
@@ -290,26 +291,26 @@ exports.set = testCase({
},
basic: function (test) {
- var tmpl8 = swig.fromString('{% set foo = "bar" %} {{ foo }}');
- test.strictEqual(tmpl8.render({}), ' bar');
+ var tpl = swig.compile('{% set foo = "bar" %} {{ foo }}');
+ test.strictEqual(tpl({}), ' bar');
test.done();
},
'from var': function (test) {
- var tmpl8 = swig.fromString('{% set foo = bar|lower %} {{ foo }}');
- test.strictEqual(tmpl8.render({ bar: 'BAR' }), ' bar');
+ var tpl = swig.compile('{% set foo = bar|lower %} {{ foo }}');
+ test.strictEqual(tpl({ bar: 'BAR' }), ' bar');
test.done();
},
'array': function (test) {
- var tmpl8 = swig.fromString('{% set foo = ["hi", "bye"] %} {{ foo[0] }}');
- test.strictEqual(tmpl8.render({}), ' hi');
+ var tpl = swig.compile('{% set foo = ["hi", "bye"] %} {{ foo[0] }}');
+ test.strictEqual(tpl({}), ' hi');
test.done();
},
'object': function (test) {
- var tmpl8 = swig.fromString('{% set foo = { bar: "bar" } %} {{ foo.bar }}');
- test.strictEqual(tmpl8.render({}), ' bar');
+ var tpl = swig.compile('{% set foo = { bar: "bar" } %} {{ foo.bar }}');
+ test.strictEqual(tpl({}), ' bar');
test.done();
}
});
@@ -321,26 +322,26 @@ exports.macro = testCase({
},
basic: function (test) {
- var tmpl8 = swig.fromString('{% macro foo %}hi!{% endmacro %}oh, {{ foo }}');
- test.strictEqual(tmpl8.render({}), 'oh, hi!');
+ var tpl = swig.compile('{% macro foo %}hi!{% endmacro %}oh, {{ foo }}');
+ test.strictEqual(tpl({}), 'oh, hi!');
test.done();
},
args: function (test) {
- var tmpl8 = swig.fromString('{% macro foo input %}{{ input }}{% endmacro %}oh, {{ foo("yep") }}');
- test.strictEqual(tmpl8.render({}), 'oh, yep');
+ var tpl = swig.compile('{% macro foo input %}{{ input }}{% endmacro %}oh, {{ foo("yep") }}');
+ test.strictEqual(tpl({}), 'oh, yep');
test.done();
},
complex: function (test) {
- var tmpl8 = swig.fromString([
+ var tpl = swig.compile([
'{% macro input type name id label value %}',
'<label for="{{ name }}">{{ label }}</label>',
'<input type="{{ type }}" name="{{ name }}" id="{{ id }}" value="{{ value }}">',
'{% endmacro %}',
'{{ input("text", "person", person.id, "Your Name") }}'
].join(''));
- test.strictEqual(tmpl8.render({
+ test.strictEqual(tpl({
person: { id: 'asdf', name: 'Paul' }
}), '<label for="person">Your Name</label><input type="text" name="person" id="asdf" value="">');
test.done();
@@ -348,23 +349,23 @@ exports.macro = testCase({
'import': function (test) {
if (typeof window !== 'undefined') {
- swig.fromString('{% macro foo %}\nhi!\n{% endmacro %}\n\n{% macro bar baz %}\n{% if baz %}\nbye!\n{% else %}\nfudge.\n{% endif %}\n{% endmacro %}', 'macros.html');
+ swig.compile('{% macro foo %}\nhi!\n{% endmacro %}\n\n{% macro bar baz %}\n{% if baz %}\nbye!\n{% else %}\nfudge.\n{% endif %}\n{% endmacro %}', { filename: 'macros.html' });
}
- var tpl = swig.fromString('{% import "macros.html" as blah %}{{ foo }}');
- test.strictEqual(tpl.render({}), '', 'importing as context does not override base context');
+ var tpl = swig.compile('{% import "macros.html" as blah %}{{ foo }}');
+ test.strictEqual(tpl({}), '', 'importing as context does not override base context');
- tpl = swig.fromString('{% import "macros.html" as blah %}{{ blah.foo }}, {{ blah.bar("baz") }}');
- test.strictEqual(tpl.render({}), '\nhi!\n, \n\nbye!\n\n', 'basic macros imported');
+ tpl = swig.compile('{% import "macros.html" as blah %}{{ blah.foo }}, {{ blah.bar("baz") }}');
+ test.strictEqual(tpl({}), '\nhi!\n, \n\nbye!\n\n', 'basic macros imported');
test.done();
},
'import in parent template': function (test) {
- swig.fromString('{% macro foo input %}hey, {{ input }}{% endmacro %}', 'blarbar.html');
- swig.fromString('{% import "blarbar.html" as mahmacros %} foobar {% block content %}{% endblock %}', 'parent.html');
+ swig.compile('{% macro foo input %}hey, {{ input }}{% endmacro %}', { filename: 'blarbar.html' });
+ swig.compile('{% import "blarbar.html" as mahmacros %} foobar {% block content %}{% endblock %}', { filename: 'parent.html' });
- var tpl = swig.fromString('{% extends "parent.html" %} {% block content %}{{ mahmacros.foo("bar") }}{% endblock %}');
+ var tpl = swig.compile('{% extends "parent.html" %} {% block content %}{{ mahmacros.foo("bar") }}{% endblock %}');
- test.strictEqual(tpl.render({}), ' foobar hey, bar');
+ test.strictEqual(tpl({}), ' foobar hey, bar');
test.done();
}
});
@@ -376,20 +377,20 @@ exports.filter = testCase({
},
basic: function (test) {
- var tpl = swig.fromString('{% filter capitalize %}oh HEY there{% endfilter %}');
- test.strictEqual(tpl.render({}), 'Oh hey there');
+ var tpl = swig.compile('{% filter capitalize %}oh HEY there{% endfilter %}');
+ test.strictEqual(tpl({}), 'Oh hey there');
test.done();
},
'complex content': function (test) {
- var tpl = swig.fromString('{% filter capitalize %}oh {{ foo }} {% if here %}here{% else %}there{% endif %}{% endfilter %}');
- test.strictEqual(tpl.render({ foo: 'HEY', here: true }), 'Oh hey here');
+ var tpl = swig.compile('{% filter capitalize %}oh {{ foo }} {% if here %}here{% else %}there{% endif %}{% endfilter %}');
+ test.strictEqual(tpl({ foo: 'HEY', here: true }), 'Oh hey here');
test.done();
},
args: function (test) {
- var tpl = swig.fromString('{% filter replace "\\." "!" "g" %}hi. my name is paul.{% endfilter %}');
- test.strictEqual(tpl.render({}), 'hi! my name is paul!');
+ var tpl = swig.compile('{% filter replace "\\." "!" "g" %}hi. my name is paul.{% endfilter %}');
+ test.strictEqual(tpl({}), 'hi! my name is paul!');
test.done();
}
});
View
48 tests/templates.test.js
@@ -6,7 +6,7 @@ exports.Errors = testCase({
swig.init({ allowErrors: true });
test.throws(function () {
- swig.fromString('{% for foo in blah %}{% endif %}');
+ swig.compile('{% for foo in blah %}{% endif %}');
}, Error);
test.done();
},
@@ -19,9 +19,9 @@ exports.Errors = testCase({
}}
});
- var tmpl8 = swig.fromString('{% foo %}');
+ var tpl = swig.compile('{% foo %}');
test.throws(function () {
- tmpl8.render({});
+ tpl({});
}, Error);
test.done();
},
@@ -30,7 +30,7 @@ exports.Errors = testCase({
swig.init({});
test.doesNotThrow(function () {
- swig.fromString('{% for foo in bar %}{% endif %}');
+ swig.compile('{% for foo in bar %}{% endif %}');
}, Error);
test.done();
},
@@ -42,9 +42,9 @@ exports.Errors = testCase({
}}
});
- var tmpl8 = swig.fromString('{% foobar %}');
+ var tpl = swig.compile('{% foobar %}');
test.doesNotThrow(function () {
- tmpl8.render({});
+ tpl({});
}, Error);
test.done();
}
@@ -58,14 +58,14 @@ exports.Variables = testCase({
}
}});
- var tmpl8 = swig.fromString('{{ asdf|lower }}');
- test.strictEqual(tmpl8.render({ asdf: 'BLAH' }), 'blah');
+ var tpl = swig.compile('{{ asdf|lower }}');
+ test.strictEqual(tpl({ asdf: 'BLAH' }), 'blah');
- tmpl8 = swig.fromString('{{ date|date("F jS, Y") }}');
- test.strictEqual(tmpl8.render({ date: new Date(2011, 8, 24) }), 'September 24th, 2011');
+ tpl = swig.compile('{{ date|date("F jS, Y") }}');
+ test.strictEqual(tpl({ date: new Date(2011, 8, 24) }), 'September 24th, 2011');
- tmpl8 = swig.fromString("{{ date|date('F jS, Y') }}");
- test.strictEqual(tmpl8.render({ date: new Date(2011, 8, 24) }), 'September 24th, 2011');
+ tpl = swig.compile("{{ date|date('F jS, Y') }}");
+ test.strictEqual(tpl({ date: new Date(2011, 8, 24) }), 'September 24th, 2011');
test.done();
},
@@ -76,25 +76,25 @@ exports.Variables = testCase({
}
}});
- var tmpl8 = swig.fromString('{{ asdf|foo }}');
- test.strictEqual(tmpl8.render({ asdf: 'blah' }), 'bar');
+ var tpl = swig.compile('{{ asdf|foo }}');
+ test.strictEqual(tpl({ asdf: 'blah' }), 'bar');
test.done();
},
'notation': function (test) {
swig.init({});
- var tmpl8 = swig.fromString('{{ a.b.c }}');
- test.strictEqual(tmpl8.render({ a: { b: { c: 'hi' }} }), 'hi', 'dot notation');
+ var tpl = swig.compile('{{ a.b.c }}');
+ test.strictEqual(tpl({ a: { b: { c: 'hi' }} }), 'hi', 'dot notation');
- tmpl8 = swig.fromString('{{ a[0][1] }}');
- test.strictEqual(tmpl8.render({ a: [['no', 'yes']] }), 'yes', 'array notation');
+ tpl = swig.compile('{{ a[0][1] }}');
+ test.strictEqual(tpl({ a: [['no', 'yes']] }), 'yes', 'array notation');
- tmpl8 = swig.fromString('{{ a[0].b[1] }}');
- test.strictEqual(tmpl8.render({ a: [{ b: ['no', 'yes'] }] }), 'yes', 'mixed notation');
+ tpl = swig.compile('{{ a[0].b[1] }}');
+ test.strictEqual(tpl({ a: [{ b: ['no', 'yes'] }] }), 'yes', 'mixed notation');
- tmpl8 = swig.fromString('{{ a["b"].b[1] }}');
- test.strictEqual(tmpl8.render({ a: { b: { b: ['no', 'yes'] }} }), 'yes', 'mixed notation');
+ tpl = swig.compile('{{ a["b"].b[1] }}');
+ test.strictEqual(tpl({ a: { b: { b: ['no', 'yes'] }} }), 'yes', 'mixed notation');
test.done();
}
@@ -103,8 +103,8 @@ exports.Variables = testCase({
exports['double-escape forward-slash'] = function (test) {
swig.init({});
- var tpl = swig.fromString('foobar\\/');
- test.strictEqual(tpl.render({}), 'foobar\\/');
+ var tpl = swig.compile('foobar\\/');
+ test.strictEqual(tpl({}), 'foobar\\/');
test.done();
};
Please sign in to comment.
Something went wrong with that request. Please try again.