Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Filling in custom tags documentation better

  • Loading branch information...
commit ec2ad17593ad94e4214b483b2d70935fa519a87f 1 parent f0973a4
@paularmstrong authored
Showing with 61 additions and 5 deletions.
  1. +57 −5 docs/custom-tags.md
  2. +4 −0 docs/tags.md
View
62 docs/custom-tags.md
@@ -23,12 +23,64 @@ First, include the Swig parser and helpers.
var parser = require('swig/lib/parser'),
helpers = require('swig/lib/helpers');
-## Useful Methods
+Define your tag and whether or not it requires an "end" tag:
-To bind a variable in your current scope into the templates scope:
+ exports.mytag = function (indent) {
+ return 'output';
+ };
+ exports.mytag.ends = true;
+
+## Something Really Simple
+
+To parse a swig variable with or without filters into a variable token, eg. `bar` or `foo|lowercase`
+
+ exports.mytag = function (indent) {
+ 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:
+
+ exports.mytag = function (indent) {
+ var myArg = parser.parseVariable(this.args[0]),
+ output = '';
+ output += helpers.setVar(name, myArg);
+ return output;
+ };
+ exports.mytag.ends = true;
+
+To parse the inner content of a tag for outputting:
+
+ exports.mytag = function (indent) {
+ var myArg = parser.parseVariable(this.args[0]),
+ output = [];
+
+ output.push(helpers.setVar('__myArg', myArg));
+
+ output.push('<h1>');
+ output.push('__output.push(__myArg)');
+ output.push('</h1>');
+ output.push('<p>');
+ output.push(parser.compile.call(this, indent + ' '));
+ output.push('</p>');
+
+
+ return output.join('\n' + indent);
+ };
+ exports.mytag.ends = true;
+
+When using your tag, it might have the following effect (assume `blah` is equal to "Scrumdiddlyumptious"):
+
+Template:
+
+ {% mytag blah %}Tacos{% endmytag %}
+
+Output:
- helpers.setVar(name, value);
+ <h1>Scrumdiddlyumptious</h1>
+ <p>Tacos</p>
-To parse a swig variable with or without filters, eg. `bar` or `foo|lowercase`
+## Write Your Own
- var myArg = parser.parseVariable(this.args[0]);
+To best understand how to write your own tag, reference `swig/lib/tags.js` to see how the internal tags are written. These will give you a pretty clear indication of how to write your own.
View
4 docs/tags.md
@@ -151,3 +151,7 @@ Your output may look like this:
<label for="lname">Last Name</label>
<input type="text" name="lname" id="lname" value="" class="error">
</div>
+
+## Custom Tags
+
+Swig has support for you to write your own custom tags. For more information, see the [Custom Tags Documentation](custom-tags.md).
Please sign in to comment.
Something went wrong with that request. Please try again.