mew-template

0.1.0 • Public • Published

mew-template

precompile template to a cmd module, support jst doT Handlebars xtemplate

Install

npm install mew-template

Usage

var template = require('mew-teplate');
var html = template(tmplContent, options);
 
options = {
    engine: 'dot', // dot jst handlebars xtemplate
    syntax: '{{}}' // {{}} or <%%>  only works with dot & jst
 }

Sample

template

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{{= it.title }}</title>
</head>
<body>
    <h1>Hello,{{= it.name }}</h1>

    <h2>Condition</h2>
    {{? it.name.length }}
    <p>{{= it.name }} is a good name</p>
    {{? }}

    <h2>Array</h2>
    <ul>
    {{~ it.skills :val:idx}}
    <li>{{= idx }}. {{= val }}</li>
    {{~ }}
    </ul>
</body>
</html>

compiled

define(function() {
    var template = function anonymous(it) {
        var out = '<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>' + (it.title) + '</title></head><body> <h1>Hello,' + (it.name) + '</h1> <h2>Condition</h2> ';
        if (it.name.length) {
            out += ' <p>' + (it.name) + ' is a good name</p> ';
        }
        out += ' <h2>Array</h2> <ul> ';
        var arr1 = it.skills;
        if (arr1) {
            var val, idx = -1,
                l1 = arr1.length - 1;
            while (idx < l1) {
                val = arr1[idx += 1];
                out += ' <li>' + (idx) + '' + (val) + '</li> ';
            }
        }
        out += ' </ul></body></html>';
        return out;
    }
    return {
        render: function(data) {
            return template(data || {});
        }
    };
});
 

Test

make test

Package Sidebar

Install

npm i mew-template

Weekly Downloads

3

Version

0.1.0

License

MIT

Last publish

Collaborators

  • l3au