More than a mustache.
npm install beardMore than a mustache.
* Clean syntax
* Ability to dynamically cache templates
* Cached rendered templates for faster renders
npm install beard
`` js
const data = {
noun: 'beards',
capitalize: str => str.charAt(0).toUpperCase() + str.slice(1)
};
const beard = require('beard');
const engine = beard({
templates: {
'/example': '{{capitalize(noun)}} are itchy.'
}
});
const result = engine.render('/example', data);
console.log(result); // returns 'Beards are itchy.'
`
opts (object) - An object literal with the following optional engine options:
- templates (object) - An object literal containing your templates index.
- root (string) - The absolute path to the root directory where all templates are stored. If you provide a root directory, beard will create your templates cache for you.
- home (string) - Relative path to home directory (used via '~' in paths, E.g. '~/layout').false
- cache (boolean) - Set to to disable caching of template files. Defaults to true.asset
- asset (function) - Callback used for tag. Looks up asset paths. See asset example below.
path (string) - A string to be parsed and populated by the view object.
locals (object) - An object of data and/or methods which will populate the template string.
` js
const beard = require('beard');
const engine = beard({
templates: {
'/layout': 'header | {{view}} | footer',
'/app/page/content': "{{extends '/layout'}}content {{include '~/component'}}",
'/app/component': 'and component'
},
root: '/',
home: 'app/',
cache: true
});
const result = engine.render('/app/page/content');
console.log(result); // returns 'header | content and component | footer'
`
``
{{include 'template'}}
{{include 'template', {arg: 'val', arg2: 'val2'}}}
{{include 'template', {
arg1: 'val1',
arg2: 'val2'
}}}
layout.brd.html
``
{{view}}
and rendering:
``
{{extends 'layout'}}view content
Returns:
``
view content
callback option.`
engine = beard({
asset: path => '/assets/' + path
});
`Used in a template:
`
`Returns:
`
`$3
The put tag outputs a local variable or a block, or an empty string if the value doesn't exist.
`
{{put foo}}
`This will output the value of
foo, if defined, or a blank string if not. Conversely, accessing it
directly, such as {{foo}} would raise an error if it were undefined.$3
Make content available for rendering in any context (such as an extended layout or an included partial.)`
{{block middle}}
Middle
{{endblock}}Top
{{middle}}
Bottom
`Returns:
`
Top
Middle
Bottom
`You can also conditionally check if a block is set.
`
{{block cart}}
everything you have put in your cart...
{{endblock}}// another template
{{exists cart}}
{{cart}}
{{else}}
Your cart is empty.
{{end}}
`Will returns:
`
everything you have put in your cart...
`Using
put is a simple way to output the block content if you are unsure if it has been set:`
{{put header}}
`This will output an empty string if the header has not been set.
$3
`
{{if x === 1}}
x is 1
{{else if x > 1}}
x is greater than 1
{{else}}
x is less than 1
{{end}}
`$3
Iterate over properties in object.`
{{for key, value in object}}
{{key}} = {{value}}
{{end}}
`#### each loop
Iterate over array.
`
{{each item in array}}
{{item.property}}
{{end}}
``* keeto (Mark Obcena) for the first iteration of the parser/compiler
* joeosburn (Joe Osburn) for the updated compiler, cached compiled functions, tests, and benchmarks
Released under MIT license.