Literal Template String Render
Use Javascript Literal Template String syntax for file templates.
ltsr = new LTSR({root: , layout: })
: create a renderer at a specific root, optionally with a default layout.
ltsr.render(file, { locals:, collection:, sep:, keyName:, valueName:, keepWhitespace, layout:})
render
second argument defaults:
locals = {}
collection = null
sep = ''
: String to place between collection elementskeyName = 'index'
: When collection is aSet
or anArray
.keyName = 'key'
: Otherwise.valueName = 'value'
keepWhitespace = false
: set to true to keep trailing whitespace from the template filelayout = undefined
: see Layout section below
ltsr.raw(file, keepWhitespace = false)
may be used to render the template without interpolation, useful for including javascript files which have string templates in them.
It also allows the file (on disk) to not include the .lt
extension. ltsr.raw("style.css")
(or render.raw
from within a template) will first attempt to render style.css.lt
, if that fails it will attempt to render style.css
.
render(file, { locals: {} })
: render template at root/file
making the keys in locals
available as arguments to the template
With collection: (Array or Set)
: repetitively render template at root/file
making the keys in locals
available as arguments to the template. Collection is iterated and the index and values are sent as the valueName
and keyName
respectively.
Effectively making these lines equivalent:
['a', 'b', 'c'].map((value, index) => ltsr.render('template', { locals: { content: value, position: index } })).join('');
ltsr.render('template', {collection: ['a', 'b', 'c'], valueName: 'content', keyName: 'position'}));
The following are also equivalent, using the default value and key names for arrays:
['a', 'b', 'c'].map((value, index) => ltsr.render('template', { locals: { value, index } })).join('');
ltsr.render('template', { collection: ['a', 'b', 'c'] }));
With collection: (Map or Object)
: as above, but the key is passed where the index would have been above.
<div>Hello ${name} of ${place}</div>
Called with render('partial', {locals: {name: 'Hooky', place: 'Earth'}})
:
<div>Hello Hooky of Earth</div>
Called with render('partial', {collection: ['Hooky', 'TJ'], locals: {place: 'Earth'}, valueName: 'name'})
:
<div>Hello Hooky of Earth</div>
<div>Hello TJ of Earth</div>
- Note: the array index is passed to the partial (with the default key
'index'
), but is not used in this case.
Called with render('partial', {collection: {Hooky: 'Earth', TJ: 'Earth', Marvin: 'Mars'}, keyName: 'name', valueName: 'place'})
:
<div>Hello Hooky of Earth</div>
<div>Hello TJ of Earth</div>
<div>Hello Marvin of Mars</div>
The render and render.raw methods are available to templates, enabling partial renders:
<html>
<body>
${ render('partial', {locals: user}) }
</body>
</html>
<html>
<body>
<div>Hello Hooky of Earth</div>
</body>
</html>
Providing a layout template to render
will wrap the target rendered file in the layout at the point of a yield
.
render
and raw
may be called within the layout file, but note that none of the values sent to the template which invoked the layout are provided. (you may get them using the yield 'name'
technique shown below).
<html>
<head><title>${yield 'title'}</title></head>
<body>${yield}</body>
</html>
This is the body
<html>
<head><title>This is the title</title></head>
<body>This is the body</body>
</html>
Unlike rendering a partial, if a value yielded for (such as yield 'title'
in this case) is not provided, LTSR will not raise an error.
No arguments are passed to the layout, but can be provided in the locals
field of the render options. Note that they must be yield
ed for.
If render is called with a layout while rendering a collection, each item is wrapped in the layout.
When rendering a collection, you can provided an element separator string to place between items.
<div>${name}</div>
Calling with render('partial', { collection: ['me', 'you', 'her', 'him'], valueName: 'name', sep: '<hr/>\n'})
:
<div>me</div><hr/>
<div>you</div><hr/>
<div>her</div><hr/>
<div>him</div>
Calling ltsr.render
and ltsr.raw
will only load the provided file from the provided root
.
Calling render
or render.raw
from within a template will attempt to load the file first from {root}/partial/{template}.lt
, then {root}/{template}.lt
. Calling raw will also attempt to load (in the same order) files without the .lt
extension after trying with.
Using the layout
option will attempt to load first from {root}/layout/{template}.lt
then {root}/{template}.lt
. Note that calling render
or render.raw
from within a layout
will use the same paths described above, even if the layout was found in the layout/
sub-directory.