Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

starting custom-tags docs

  • Loading branch information...
commit b226383d8df80671c210560d1fceebe207280f80 1 parent 940b138
@paularmstrong authored
Showing with 34 additions and 20 deletions.
  1. +34 −0 docs/custom-tags.md
  2. +0 −20 docs/tags.md
View
34 docs/custom-tags.md
@@ -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
20 docs/tags.md
@@ -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).
Please sign in to comment.
Something went wrong with that request. Please try again.