$ npm install xtpl
nodejs wrapper around xtemplate engine (easier for expressjs and koajs)
refer: https://github.com/xtemplate/xtemplate
Object config(option:Object)
option details:
name | type | default | description |
---|---|---|---|
encoding | String | utf-8 | xtpl file encoding |
XTemplate | Object | require('xtemplate') | xtemplate module value |
if options is undefined, then this method will return global config.
void renderFile(path:String, options:Object, callback:function)
parameter details:
name | type | default | description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
path | String | xtpl template file | |||||||||||||
option | Object |
data to be rendered. the following properties will be used for control.
|
|||||||||||||
callback | function | callback |
npm install xtpl xtemplate --save
var xtpl = require('xtpl');
xtpl.renderFile('./x.xtpl',{
x:1
},function(error,content){
});
xtpl.__express = xtpl.renderFile
clear xtemplate cache cached by xtpl file path
void clearCache(path:String);
var app = require('express')();
app.set('views','./views');
app.set('view engine', 'xtpl');
app.use(function(req, res){
res.render('test',{data:1});
});
var app = require('xtpl/lib/koa2')(new require('koa')(),{
views:'./views'
});
app.use(async function(ctx){
await ctx.render('test',{data:1});
});
├── footer.xtpl
├── header.xtpl
├── index.xtpl
├── layout.xtpl
├── layout1.xtpl
└── sub
└── header.xtpl
index.xtpl
{{extend ("./layout1")}}
{{#block ("head")}}
<!--index head block-->
<link type="text/css" href="test.css" rev="stylesheet" rel="stylesheet" />
{{/block}}
{{#block ("body")}}
<!--index body block-->
<h2>{{title}}</h2>
{{/block}}
layout1.xtpl
<!doctype html>
<html>
<head>
<meta name="charset" content="utf-8" />
<title>{{title}}</title>
{{{block ("head")}}}
</head>
<body>
{{{include ("./header")}}}
{{{block ("body")}}}
{{{include ("./footer")}}}
</body>
</html>
render
res.render("index", {title: "xtpl engine!"})
output
<!doctype html>
<html>
<head>
<meta name="charset" content="utf-8" />
<title>xtpl engine!</title>
<!--index head block-->
<link type="text/css" href="test.css" rev="stylesheet" rel="stylesheet" />
</head>
<body>
<h1>header</h1>
<h2>sub header</h2>
<!--index body block-->
<h2>xtpl engine!</h2>
<h1>footer</h1>
</body>
</html>
https://github.com/xtemplate/xtpl/milestones
xtpl is released under the MIT license.
© 2010 - cnpmjs.org x YWFE | Home | YWFE