Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Commit

Permalink
starting some custom tags examples
Browse files Browse the repository at this point in the history
  • Loading branch information
paularmstrong committed Sep 23, 2011
1 parent 2cf5f28 commit 940b138
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
28 changes: 28 additions & 0 deletions examples/custom_tags/custom_tags.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions examples/custom_tags/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% for person in people %}
{% input "text" person.id 'Name' person.name %}
{% endfor %}
24 changes: 24 additions & 0 deletions examples/custom_tags/server.js
Original file line number Diff line number Diff line change
@@ -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/');

0 comments on commit 940b138

Please sign in to comment.