Intro

Nbtemp is dumb and probably isn't what you're looking for. It's a template engine with no frills—it's just one small file and no dependencies. It either runs in your environment, or it doesn't.

What can it do? All the normal templating stuff: interpolate, flow control, includes, and blocks.

What it will never do? File IO, impose a stucture or a style on your project, be packaged for NPM, touch the DOM or use any JS feature restricted to a browser.

Don't use Nbtemp! It isn't what you're looking for. I made it to embed in an app I'm working on.

API

Create template functions using the Template constructor.

var t = new Template(source)
A template function t()
var n = new Template(name, source)
A template function n() named name
var c = new Template([src1, src2])
A template function c() composed of src1 and by applying the blocks in src2
var nc = new Template(name, [src1, src2])
A template function nc() named name composed of src1 and by applying the blocks in src2

Render templates by calling the function with an argument object.

var s = t(obj)
A rendered template s as a string
var r = t({ one: 1, two: 2 })
A rendered template r as a string
element.innerHTML = t({ one: 1, two: 2 })
A rendered template inserted into element

Tags

Nbtemp tags are where you tell the template compiler to stop copying and to start doing some processing. All tags start with <% and end with %>.

Description

Nbtemp has several tags.

<% %>
Statement tag
<%= %>
Interpolate tag
<%# %>
Comment tag

Example


    

Output


    

Environment

The environment is the universe of things that can be referenced from the current template. This includes the argument object. The contents of the argument object are accessed through the @ symbol.

If you want to dump the environment place <% env() %> somewhere in your template.

Using the argument object

Given an argument object arg


      

and a template function t() called with the argument object t(arg)

@a
evaluates to 1
@b
evaluates to 2
@c[0]
evaluates to 10

Example


      

    

Output


    

Arguments

Includes

Blocks

The special $content block

Any conent outside of a block region will go in the designated content block.

Example template1.html


    

Example template2.html


    

Output


    

More examples

Tests

Acknowledgments

License

MIT LICENSE

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.