0.0.9 • Published 10 years ago

zt-template v0.0.9

Weekly downloads
2
License
MIT
Repository
github
Last release
10 years ago

zt-template

zt-template is a simple template for node.js, which supports both Express and Koa.

Features

  • Extends: allows you to extend parent template.
  • Flow Control: supports these statements: if|for|else|switch|case|break .
  • Cache: supports two level cache, complied template and final parsed html.

Installation

$ npm install zt-template

Usage

Express

var template = require('zt-template/express');
var viewsPath = path.join(__dirname, 'views');
var te = template(viewsPath);
app.engine('html',te );
app.set('view engine', 'html');

app.get('/', function (req, res, next) {
    res.render('index', {
            name: 'Tiger',
            skills: ['Java', 'PHP', 'C#', "Node.js"]
        }
    );
});

Koa

var router = require('koa-router')();
var template = require('zt-template/koa');
app.use(template(path.join(__dirname, 'views')));
router.get('/', function *(next) {
    yield this.render('index', {
            name: 'Tiger',
            skills: ['Java', 'PHP', 'C#', "Node.js"]
        });
});

Options

  • root: the directory which contains all views. This option is mandatory.
  • extension: the extension of the view file. Default is .html.
  • tag.open & tag.close: the open and close tag. Default is <% & %>.
  • cache.time: cache manager will remove expired items in cache per this value. Default is 5 mins.
  • cache.min: the min items in cache. If cached size < this value, cache manager will not remove item from cache. Default is 100
  • cache.max: the max items in cache. If cached size > this vlaue, cache manager will not accept new items. Default is 10000.

Example:

......
var settings = {
    root: template(path.join(__dirname, 'views')),
    extension: ".html",
    cachetime: 5 * 60 * 1000,
    tag: {
        open: '<%',
        close: '%>'
    },
    cache: {
        min: 100,
        max: 10000
    }
};

var te = template(settings);
......

Parent Template:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>It is Tiger</title>
</head>
<body>

<div>
    <%@ block="block_header" %>
</div>

<div>
    <%@ block="block_footer" %>
</div>

</body>
</html>

Child Template:

<%@ extends="layout.html" %>

<% block="block_header" %>
Name : <%=name%>
<% endblock %>

<% block="block_footer" %>
<ul>
<% for (var i=0;i<skills.length;i++){%>
  <li><%=skills[i]%></li>
<%}%>
</ul>
<% endblock %>

TODO

  • Watch: Refresh cache when any template file is changed.
  • *Include: Include file

Licences

MIT

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago