`typo` is an extendable template engine designed for the future
npm install typo
typo is an extendable template engine designed for the future:
- featured with Promise and async/await.
- powerful custom sync/async directives(helpers).
``sh`
$ npm install typo --save
`js`
const typo = require('typo')()
typo.template('Hello, {{user.name}}!', {
user: {
name: 'Steve'
}
}).then(console.log)
// Hello, Steve!
`js
const typo = require('typo')()
const chalk = require('typo-chalk')
typo.use(chalk)
typo.template('Once in a {{blue blue}} moon').then(console.log)
// Then it will print a blue word "blue"
`
Basic:
`js`
typo.use('upper', word => word.toUpperCase())
typo.template('{{upper foo}} bar').then(console.log)
// FOO bar
`js
typo.use('fullname', async name => await getFullNameFromServer(name))
typo.template('{{fullname name}}', {name: 'Steve'}).then(console.log)
// Steve Jobs
typo.template('{{fullname Steve}}').then(console.log)
// Steve Jobs
`
`jsOnce in a {{blue color}} moon
const template = typo.compile()
template({color: 'blue'})
.then(console.log)
// Once in a blue moon
`
Creates the typo instance.
- open String={{ The beginning of each directive.String=}}
- close The end of each directive.
Compiles a template into a function.
- template StringObject
- compile_options Boolean=true
- async whether should be compiled into an asynchronous function, defaults to trueNumber=Number.POSITIVE_INFINITY
- concurrency If compiled as an asynchronous function, the number of max concurrently pending directive functions.enum.
- value_not_defined Suppose the value of an expression is not found in data, then it will print the expression directly if print(as default), or print nothing if ignore, or throw an error if throw.enum.
- directive_value_not_defined Tells typo what to do if the parameter expression of a directive is not found in data. And this option is default to the value of value_not_defined
`js
// default options
typo.compile('{{blue color}}')()
// prints a blue letter, "color"
.then(console.log) => {
// value_not_defined: throw
typo.compile('{{blue color}}', {
value_not_defined: 'throw'
})()
.catch((e) => {
// code frame and
// 'value not found for key "color"''
console.error(e.message)
})
typo.compile('{{adjective}} {{blue color}}', {
value_not_defined: 'throw',
directive_value_not_defined: 'print'
})({
adjective: 'beautiful'
})
// prints "beautiful color", and the letter color is blue
.then(console.log)
`
Returns function(data)
async: false
`js`
const result = typo.compile(template)(data)
console.log(result)
async: true (default)
`js`
typo.compile(template)(data).then(console.log)
- template StringObject=
- data Object=
- compile_options
Returns Promise if compile_options.async is true(default), or String the substituted result if is not.
`mustache``
{{
{{
MIT