Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding import docs and macro example

  • Loading branch information...
commit a67c3a4511fdf3ab67ba43f14aae279b3911ac02 1 parent e8d6f69
@paularmstrong authored
View
2  docs/README.md
@@ -11,4 +11,6 @@
## Examples
1. [Basic Usage](https://github.com/paularmstrong/swig/tree/master/examples/basic)
+1. [Custom Tags](https://github.com/paularmstrong/swig/tree/master/examples/custom_tags)
1. [Express.js with Swig](https://github.com/paularmstrong/swig/tree/master/examples/express)
+1. [Macros](https://github.com/paularmstrong/swig/tree/master/examples/macros)
View
17 docs/tags.md
@@ -146,7 +146,6 @@ It is also possible to set variables in templates.
Macros are custom, reusable methods for content-generation that are defined in templates.
-
#### Example
One of the most common use-case for macros is form inputs. To start, define your `input` macro somewhere in your template scope (the top of a template, or an included template is a good spot):
@@ -172,6 +171,22 @@ Your output may look like this:
<input type="text" name="lname" id="lname" value="" class="error">
</div>
+### import
+
+The `import` tag is specifically designed for importing macros into your template with a specific context scope. This is very useful for keeping your macros from overriding template context that is being injected by your server-side page generation.
+
+#### Usage
+
+Assuming the macro `input` exists in _formmacros.html_, you can run the macro by using `{{ form.input }}` as follows:
+
+ {% import 'formmacros.html' as form %}
+
+ {# this will run the input macro #}
+ {{ form.input("text", "name") }}
+
+ {# this, however, will NOT output anything because the macro is scoped to the "form" object: #}
+ {{ input("text", "name") }}
+
## Custom Tags
Swig has support for you to write your own custom tags. For more information, see the [Custom Tags Documentation](custom-tags.md).
View
8 examples/macros/form.html
@@ -0,0 +1,8 @@
+{% macro input type name label %}
+<label for="{{ name }}">{{ label }}</label>
+<input type="{{ type }}" name="{{ name }}" id="{{ name }}">
+{% endmacro %}
+
+{% macro button type label %}
+<button type="{{ type|default("submit") }}">{{ label|default("Submit") }}</button>
+{% endmacro %}
View
8 examples/macros/index.html
@@ -0,0 +1,8 @@
+{% import 'form.html' as form %}
+
+<ul>
+ <li>{{ form.input("text", "name", "Your Name") }}</li>
+ <li>{{ form.input("text", "age", "Your Age") }}</li>
+</ul>
+
+{{ form.button() }}
View
23 examples/macros/server.js
@@ -0,0 +1,23 @@
+var http = require('http'),
+ swig = require(__dirname + '/../../index');
+
+swig.init({
+ root: __dirname
+});
+
+http.createServer(function (req, res) {
+ var tmpl = swig.fromFile('index.html'),
+ renderedHtml = tmpl.render({
+ people: [
+ { name: 'Paul', age: 28 },
+ { name: 'Jane', age: 26 },
+ { 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
11 tests/templates/macros.html
@@ -0,0 +1,11 @@
+{% macro foo %}
+hi!
+{% endmacro %}
+
+{% macro bar baz %}
+{% if baz %}
+bye!
+{% else %}
+fudge.
+{% endif %}
+{% endmacro %}
Please sign in to comment.
Something went wrong with that request. Please try again.