et-component
v0.0.2
Published
an easy template engine
Downloads
3
Readme
et
A carefully designed and easy to use template engine for complex client side rendering, inspired by ejs.
{{if !users.length}}
<span>no users</span>
{{else}}
<span>we have {{= users.length}} users.</span>
{{each users}}
<li>{{= name}}</li>
{{/}}
{{/}}
Inastall
npm install et-cli
Usage
var et = require("et-cli");
...
var html = et(template, data);
API
et(str, obj)
Render the given template str
with obj
.
et.compile(str, [options])
complie a reusable function with template str
and optional options
.
Syntax
{{keyword expression}}
for logic expression, expression is wrapped with()
in the result javascript function.{{/}}
indicate a close statement for statements with keywords each if and switch.
Here is the list of the keywords.
- each variable
for iteration an array, you can access each object property directly inside each expression, and variable i
would be the index start from 0. Browser should support Array.prototype.forEach
.
- if expression
could be ended by elif
else
or {{/}}
.
- elif expression
should have an unclosed if before.
- else
should not contain any expression.
- switch variable
switch a variable, should be followed by case
and default
.
- case variable
a break statement is added in the of block output.
- default
For value output, you can use:
{{= variable}}
for ouput escaped variable.{{! variable}}
for ouput unescaped variable.
Notice that null
and undefined
variables are rendered to empty string (not null
and undefined
).
License
(The MIT License)
Copyright (c) 2009-2014 qiming zhao [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.