EndSkin Template Engine for NodeJS and Javascript
NPM Install
npm install endskin
Quick Start
//require the modulevar EndSkin = ; //set the root folder of your view filesEndSkin; //create an endskin instancevar t = EndSkin; //prepare the data to be passed to the view filesvar items = type:1name:"aaa"name:"bbb"name:"ccc"; //use any of these three methods to pass datat;tdataitems = items;t; //call html() method to get the outputconsole; //re-use the instance, re-set datatdata = {}; //re-call html() method to get the outputconsole;
Express 3.x,4.x Support
var express = ;var app = ; //set the root folder of view filesapp; //set whether to cache templates, when developing, I suggest do not cache it//app.enable('view cache'); //set endskin the engine for .html filesapp; app; //start express serverapp;
Methods
module.setRoot(<String> Root folder path);
module.create(<String> view file name in Root folder);
the create() method returns an endskin instance. You can use this instance many times.
endskin Instance
Properties
<Object> endskin.data
This is the data object used when you call html() method.
You can set this property to any value.
Methods
endskin.assign(<Object>);
The keys in the object passed to assign() will be copied to endskin.data
endskin.assign(<String> key, value);
This method will add a key to the endskin.data
To pass data to an endskin instance. You can use any of the three ways:
endskin.data.key = value;
endskin.assign(key,value);
endskin.assign({ key: value });
To clear data and reuse the instance. You can set endskin.data to an empty object.
endskin.data = {};
endskin.html();
This method will generate output of the endskin instance.
Syntax in view file
Variables
{$varname}
this will show the data you set to `endskin.data.varname`
{$obj.keyname} will show `endskin.data.obj.keyname`
<%=this.data.varname%> will show `endskin.data.varname`
<%=global.varname%> will show `global.varname` ( the nodejs runtime global )
<%=new Date().toString()%> will show the current datetime string
if/else
{if($varname)}
...
{/if}
{if($varname == 1 || $varname <= -1)} ... {/if}
{if($items.length > 0)} ... {else} ... {/if}
{if($price > 100)} ... {elseif($price > 10)} ... {else} ... {/if}
Loops
when: endskin.data.items = [{type:1,name:"aaa"},{name:"bbb"},{name:"ccc"}];
{foreach($items as $item)}
{$item.name}
{/foreach}
{foreach($items as $i=>$item)}
{$i}. {$item.name}
{/foreacn}
templates including
{include sub-template.html}
or
<!-- include sub-template.html -->
the file name after "include" is based on the Root folder you set with `EndSkin.setRoot()`
You can include any amount of templates. You can also use include in sub-template.html.
Insert Native Javascript Code Snippets
<%
var a=1;
a++;
var b = this.data.b;
output.push(a+','+b);
%>
everything in <% %> will be parsed as native code snippets.
to access the data you set in endskin.data, just use this.data
to write output back, just push the string to output array.
for convenience, <%=abc%> will be parsed as <% output.push(abc); %>
Browser Support
Tested in IE6+, Chrome, Firefox, Opera, Safari
Technical Support
Chunlong longbill.cn@gmail.com, http://jszen.com