Permalink
Browse files

Merge branch 'master' into dev

  • Loading branch information...
2 parents 1d53aaf + b226383 commit f05ee116dc549218fa855a6df11c985f3f94b48f @paularmstrong committed Sep 24, 2011
Showing with 93 additions and 22 deletions.
  1. +34 −0 docs/custom-tags.md
  2. +0 −20 docs/tags.md
  3. +28 −0 examples/custom_tags/custom_tags.js
  4. +3 −0 examples/custom_tags/page.html
  5. +24 −0 examples/custom_tags/server.js
  6. +4 −2 tests/speed.js
View
@@ -0,0 +1,34 @@
+# Writing Custom Tags
+
+Swig makes it easy to write custom tags specific for your project.
+
+First, make sure to include your node.js file that declares your tags in the swig init:
+
+ swig.init({ tags: require('mytags') });
+
+Each tag will be executed with its scope bound to the tag token object. A token for a tag will look like this:
+
+ // Assume your template has {% mytag foo bar %}{% endmytag %}
+ var token = {
+ type: LOGIC_TOKEN, // Used internally by the parser. It will always be the same, no matter what tag.
+ name: 'mytag',
+ args: ['foo', 'bar'],
+ compile: tag_function
+ };
+
+## Requirements
+
+First, include the Swig parser and helpers.
+
+ var parser = require('swig/lib/parser'),
+ helpers = require('swig/lib/helpers');
+
+## Useful Methods
+
+To bind a variable in your current scope into the templates scope:
+
+ helpers.setVar(name, value);
+
+To parse a swig variable with or without filters, eg. `bar` or `foo|lowercase`
+
+ var myArg = parser.parseVariable(this.args[0]);
View
@@ -121,23 +121,3 @@ It is also possible to set variables in templates.
{% for num in foo %}
<li>{{ num }}</li>
{% endfor %}
-
-## Writing Custom Tags
-
-Swig makes it easy to write custom tags specific for your project.
-
-First, make sure to include your node.js file that declares your tags in the swig init:
-
- swig.init({ tags: require('./mytags.js') });
-
-Each tag will be executed with its scope bound to the tag token object. A token for a tag will look like this:
-
- // Assume your template has {% mytag foo bar %}{% endmytag %}
- var token = {
- type: LOGIC_TOKEN, // Used internally by the parser. It will always be the same, no matter what tag.
- name: 'mytag',
- args: ['foo', 'bar'],
- compile: tag_function
- };
-
-Now you can write a tag called `mytag` that returns a bit of JavaScript logic to have run while rendering your template. For more information on how to write a tag, view the [tags.js source file](../lib/tags.js).
@@ -0,0 +1,28 @@
+var parser = require('../../lib/parser'),
+ helpers = require('../../lib/helpers'),
+ _ = require('underscore');
+
+exports.input = function (indent) {
+ var type = parser.parseVariable(this.args[0]),
+ name = parser.parseVariable(this.args[1]),
+ label = (this.args.length > 2) ? parser.parseVariable(this.args[2]) : name,
+ value = (this.args.length > 3) ? parser.parseVariable(this.args[3]) : '',
+ out = [];
+
+ out = ['(function() {'
+ , helpers.setVar('__name', name)
+ , helpers.setVar('__type', type)
+ , helpers.setVar('__label', label)
+ , helpers.setVar('__value', value)
+ , ' __output.push(\'<div class="input \' + __type + \'">\')'
+ , ' __output.push(\'\\n\')'
+ , ' __output.push(\'<label for="\' + __name + \'">\' + __label + \'</label>\');'
+ , ' __output.push(\'<input type="\' + __type + \'" name="\' + __name + \'" id="\' + __name + \'" value="\' + __value + \'">\');'
+ , ' __output.push(\'\\n\')'
+ , ' __output.push("</div>")'
+ , ' __output.push(\'\\n\')'
+ , '})();'];
+
+ return out.join('\n' + indent);
+};
+exports.input.ends = false;
@@ -0,0 +1,3 @@
+{% for person in people %}
+ {% input "text" person.id 'Name' person.name %}
+{% endfor %}
@@ -0,0 +1,24 @@
+var http = require('http'),
+ swig = require(__dirname + '/../../index');
+
+swig.init({
+ root: __dirname,
+ tags: require('./custom_tags')
+});
+
+http.createServer(function (req, res) {
+ var tmpl = swig.fromFile('page.html'),
+ renderedHtml = tmpl.render({
+ people: [
+ { id: 'person0', name: 'Paul', age: 28 },
+ { id: 'person1', name: 'Jane', age: 26 },
+ { id: 'person2', name: 'Jimmy', age: 45 }
+ ],
+ title: 'Basic Example'
+ });
+
+ res.writeHead(200, { 'Content-Type': 'text/html' });
+ res.end(renderedHtml);
+}).listen(1337);
+
+console.log('Application Started on http://localhost:1337/');
View
@@ -19,12 +19,14 @@ tplString = "{% for v in array %}"
+ "{% endif %}"
+ "{% endfor %}";
-console.time('Compile Template');
i = 1000;
+console.time('Compile one template 1000 times');
+d = new Date();
while (i--) {
tplS = template.fromString(tplString);
}
-console.timeEnd('Compile Template');
+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");

0 comments on commit f05ee11

Please sign in to comment.