Permalink
Browse files

starting some custom tags examples

  • Loading branch information...
1 parent 2cf5f28 commit 940b13835d3c1941ede13e739507a31b329f9bae @paularmstrong committed Sep 23, 2011
Showing with 55 additions and 0 deletions.
  1. +28 −0 examples/custom_tags/custom_tags.js
  2. +3 −0 examples/custom_tags/page.html
  3. +24 −0 examples/custom_tags/server.js
View
28 examples/custom_tags/custom_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;
View
3 examples/custom_tags/page.html
@@ -0,0 +1,3 @@
+{% for person in people %}
+ {% input "text" person.id 'Name' person.name %}
+{% endfor %}
View
24 examples/custom_tags/server.js
@@ -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.