Coder Social home page Coder Social logo

node-jqtpl's Introduction

This is a port of jQuery's Template Engine to nodejs

http://github.com/jquery/jquery-tmpl

Full API documentation of the original plugin

http://api.jquery.com/category/plugins/templates/

Note: currently not implemented: wrap tag and tmplItem method.

Philosophy is similar to django

http://docs.djangoproject.com/en/dev/topics/templates/

  • no program logic in templates
  • no embeded script language like in ejs
    1. this is evil because it enables program logic in templates
    2. bad usability
    3. because of the "var" problem in javascript

Features

  • jquery tmpl plugin conform
  • extendable - you can implement new statements
  • html escaping per default
  • simple syntax
  • tiny and fast

Installation via npm

npm install jqtpl

Run tests

$ make test

Usage

require the module

var jqtpl = require("jqtpl");

jqtpl.tmpl(markup, data, options);

Compile and render a template.

  • markup html code string
  • data object or array of data
  • options optional options object

jqtpl.template(name, tpl)

Named templates - there is a way to precompile the template using a string, so you can render this template later using its name.

// tpl
<div>${a}</div>

// code

// precompile an cache it
jqtpl.template( "templateName", tpl );
// render
jqtpl.tmpl( "templateName", {a:1} );
// you can also delete the template from cache
delete jqtpl.template["templateName"];

// output
<div>1</div>       

Local variables

  • $data - data object passed to render method
  • $item - contains $data via $item.data as well as user options - an optional map of user-defined key-value pairs.

Examples:

// tpl
<div>${ $item.someMethod() }</div>	

// code
jqtpl.tmpl( tpl, {a:1}, {
	someMethod: function(){ return 1; }
});

//output
<div>1</div> 	

Tags

${} - simple output (escaped per default)

// tpl
<div>${a}</div>

// code
jqtpl.tmpl( tpl, {a:123});

// output
<div>123</div>

${} - simple output but with array as data argument (escaped per default)

//tpl
<div>${a}</div>

// code
jqtpl.tmpl( tpl, [{a:1},{a:2},{a:3}]);

// output
<div>1</div><div>2</div><div>3</div>

${} - if property is a function - it will be called automatically (escaped per default)

// tpl
<div>${a}</div>

// code
jqtpl.tmpl( tpl, {
    a:function() {
        return 1 + 5;
    }
});

//output
<div>6</div>

{{if}} and {{else}}

// tpl
{{if a == 6}}
    <div>${a}</div>
{{else a == 5}}
	<div>5</div>
{{else}}
    <div>a is not 6 and not 5</div>    
{{/if}}

// code
jqtpl.tmpl( tpl, {a:6});

// output
<div>6</div>

// code
jqtpl.tmpl( tpl, {a:5});

// output
<div>a is not 6</div>

{{each}} looping.

// tpl
{{each(i, name) names}}
    <div>${i}.${name}</div>
{{/each}}        
	
// alternative syntax	

{{each names}}
	<div>${$index}.${$value}</div>
{{/each}}

// code
jqtpl.tmpl( tpl, {names: ["A", "B"]});

// output
<div>0.A</div><div>1.B</div>

{{html}} - there is a way to avoid escaping if you know what you do :)

// tpl
<div>{{html a}}</div>

// code
jqtpl.tmpl( tpl, {a:'<div id="123">2</div>'});

// output
<div id="123">2</div>    

{{!}} - comments.

// tpl
<div>{{! its a comment}}</div>

// code
jqtpl.tmpl( tpl );

// output
<div></div>  

{{tmpl}} - subtemplates.

Note: passing json object with 2 curly brackets without any separation will break the engine: {{tmpl({a: {b: 1}}) "mypartial"}}

// tpl
<div>{{tmpl({name: "Test"}) '${name}'}}</div>

// code
jqtpl.tmpl(tpl);

// output
<div>Test</div>

Not jquery-tmpl compatible stuff

Express specific stuff

Usage

app.set("view engine", "html");
app.register(".html", require("jqtpl").express);

{{partial}} tag

Read express documentation here http://expressjs.com/guide.html#res.partial()

// tpl

// myaction.html
<div>{{partial({test) "mypartial"}}</div>

// mypartial.html
${name}

// code
app.get('/myaction', function(req, res) {
	res.render('myaction', {test: {name: 'Test'}});
})

// output
<div>Test</div> 

Using array of data:

// tpl

// myaction.html
<div id="main">
	{{partial(test) "mypartial"}}
</div>

// mypartial.html
<div class="partial">
	${name}
</div>

// code
app.get('/myaction', function(req, res) {
	res.render('myaction', {
		as: global,
		test: [
			{name: "Test1"}, 
			{name: "Test2"}
		]			
	});
})
	
// output
<div id="main">
	<div class="partial">Test1</div>
	<div class="partial">Test2</div>
</div>

{{layout}} tag

Using layout tag in a view it is possible to define a layout within this view. Note: it is possible since [email protected].

// tpl

// mylayout.html
<html>
{{html body}}
</html>

// myview.html
{{layout "mylayout"}}
	<div>myview</div> 

// output
<html>
	<div>myview</div>
</html>

{{literal}} tag

// tpl
{{literal}}
${will_not_be_evaluated}
{{/literal}}

// output

${will_not_be_evaluated}

// tpl
{{literal foo}}
{{/literal}}
{{/literal bar}}
{{/literal foo}}

// output


{{/literal}}
{{/literal bar}}

node-jqtpl's People

Contributors

kof avatar laurie71 avatar aseemk avatar garann avatar nmorse avatar defunctzombie avatar samsonjs avatar

Stargazers

McFog Wang avatar

Watchers

McFog Wang avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.