Skip to content

Repository files navigation

xtpl

nodejs wrapper around xtemplate engine (easier for expressjs and koajs)

xtplNPM downloadsBuild StatusCoverage StatusDependency Status

docs

syntax

refer: https://github.com/xtemplate/xtemplate

api

methods

config or get xtpl global option:
Objectconfig(option:Object)

option details:

nametypedefaultdescription
encodingStringutf-8xtpl file encoding
XTemplateObjectrequire('xtemplate')xtemplate module value

if options is undefined, then this method will return global config.

render file
voidrenderFile(path:String,options:Object,callback:function)

parameter details:

nametypedefaultdescription
pathStringxtpl template file
optionObject data to be rendered. the following properties will be used for control.
nametypedefaultdescription
cacheBooleanfalsewhether cache xtpl by path
setting['view encoding']Stringglobal encodingencoding for read xtpl files
callbackfunctioncallback
npm install xtpl xtemplate --save
varxtpl=require('xtpl');xtpl.renderFile('./x.xtpl',{x:1},function(error,content){});
express adaptor
xtpl.__express=xtpl.renderFile
clear cache

clear xtemplate cache cached by xtpl file path

voidclearCache(path:String);

use for expressjs

varapp=require('express')();app.set('views','./views');app.set('view engine','xtpl');app.use(function(req,res){res.render('test',{data:1});});

use for koa

varapp=require('xtpl/lib/koa2')(newrequire('koa')(),{views:'./views'});app.use(asyncfunction(ctx){awaitctx.render('test',{data:1});});

Example

├── 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>

changelog

https://github.com/xtemplate/xtpl/milestones

License

xtpl is released under the MIT license.

About

nodejs wrapper around xtemplate engine (easier for expressjs and koajs)

Resources

Stars

99 stars

Watchers

10 watching

Forks

Releases

Packages

Used by

Contributors

Languages