Coder Social home page Coder Social logo

xtpl's Introduction

xtpl

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

xtpl NPM downloads Build Status Coverage Status Dependency Status

docs

syntax

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

api

methods

config or get xtpl global option:
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.

render file
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.
name type default description
cache Boolean false whether cache xtpl by path
setting['view encoding'] String global encoding encoding for read xtpl files
callback function callback
npm install xtpl xtemplate --save
var xtpl = 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

void clearCache(path:String);

use for expressjs

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

use for koa

var app = require('xtpl/lib/koa')(require('koa')(),{
    views:'./views'
});
app.use(function*(){
    yield this.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.

xtpl's People

Contributors

yiminghe avatar wondger avatar dead-horse avatar imsobear avatar 4simple avatar

Watchers

James Cloos avatar Henry Zhuang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.