koa2-dot
doT for koa2 with support layouts, include function. It's friendly for front-end web libraries (Angular, Ember, Vue.js, React ...) example
Important
The default settings of doT has been change to use [[]]
instead of {{}}
. This to support client side templates (Angular, Ember, Vue ...)
- all the advantage of doT
- layout and include support
- uses
[[ ]]
by default, not clashing with{{ }}
(Angular, Ember...) - custom helpers to your views
- conditional, array iterators, custom delimiters...
- render, renderString, getHtmlByFile, getHtmlByString support
- all template path based on the options.root
Install
Install with npm
npm install koa2-dot --save
Then set add the middleware to koa2 app
; doThelper { let time = dateString; if time === 'Invalid Date' return time; return `年月日 时分秒`;}; app;
Layout
You can specify the layout using yaml and refer to the section as you would from a model.
You can also define any extra configurations (like a page title) that are inherited to the master.
views/layout/base.server.html
[[= layout.title ]] From base.server.html [[= layout.body ]] [[# include('partials/footer') ]]
views/layout/dot.server.html
---layout: layout/basetitle: The dot layout--- [[## body: From the dot.server.html [[= layout.content ]]##]]
views/partials/footer.html
This is footer.
views/dot/index.server.html
---layout: layout/dottitle: The example for koa2-dot--- [[## content: From index.server.html The engine of model: [[= model.engine]] The pkg of model: [[= model.pkg]] The name of app: [[= app.name]] The desc of app: [[= app.desc]] The dateFormat: [[# def.dateFormat(Date.now())]]##]]
Result
The example for koa2-dot From base.server.html From the dot.server.html From index.server.html The engine of model: doT The pkg of model: koa2-dot The name of app: koa2-dot example The desc of app: a example for koa2-dot The dateFormat: 2016年10月20日 16时55分18秒 This is footer.
advance
You can extend the render function to add app info automatically. You can refer to the example
app