node-tpl is an easy to use, fast, templating tool to template anything you want, from webpages to emails.
Basic usage
Add placeholder items
var tpl = ; tpl; tpldisplay"templates/myfile.tpl";
Create the html template
Template Demo {if new Date().getHours() >= 12} Good afternoon! {else} Good morning! {/if} {for $row in $musketeers} {$row} Age: {$row.age} Favorite Color: {$row.color} {/for}
API Documentation
Set the current directory
Files are loaded based on the current working directory (cwd). By default the cwd is set to __dirname
. This can be changed by setting it with setcwd
without a trailing /
.
tpl.setcwd("/path/to/default");
Files that are included within the markup are also loaded based on the cwd.
Variables
Variables are items that link to a variable within the template markup, they are added by using the assign
method.
Note: Assigning a variable 2 or more times will overwrite the previous variable.
tpl;tpl;
Variables can hold an array of items, including (but not limited to):
- strings
- integers
- objects
- arrays
Fetch
Fetch allows you to fetch a template and replace its values; it then gets returned back to the script. With the value you can do anything you please, such as send it in an email.
tpl;var body = tpl;console;
Display
Display allows you to fetch a template and it automatically gets displayed through process.stdout.write
.
tpl;tpldisplay"templates/page1.tpl";
Markup Documentation
Variables
Variables are prefixed with a $
and are between {
and }
. To access an array/object item use .
to get the value from array/object.
{$variable}
{$variable.array_item}
{for $var in $variable}
{if $var == $another_var}
{elseif $var == $another_var}
If statements
If statements are blocks that perform tests on a list of statements. Once one validates as true its contents will be displayed.
{if 1 == 2} This should never be displayed. {elseif '$color' == 'blue'} This will show if color equals "blue". {elseif new Date().getHours() >= 12} Good afternoon! {else} If nothing worked then you are seeing this. {/if}
If it is after hour 11
, and $color
does not equal blue
, and well 1
never equals 2
, we would then get this output:
Good afternoon!
For statements
For statements loop through a list of array items or an object and display them on a page.
{for $row in $store_items} {$row.title} {$row.description} {$row.updated} {/for}
We might then get something like this as output:
White Socks Cotton white socks 2015/01/01 Blue Socks Cotton blue socks 2015/01/25 Black Socks Silk black socks 2015/02/12
Includes
Files can be included using the include
command and included files can also include other files.
{include "templates/nav.tpl"}{include "templates/$filename"}
Note: Any file that includes itself or a parent file will probably create an endless loop.
Note: Currently a variable followed by a an extention such as $filename.tpl
will break the code.
Literals
A literal is a way to force the engine not to replace markup, because sometimes you don't always want to replace blocks, such as JavaScript in a browser, because they can sometimes mean the same.
For example, take this template markup:
When the template markup is replaced, you will get this new javascript:
Not only is it invalid JavaScript, but it isn't what you wanted.
We can fix that by placing {literal}
's around that block of code and it won't change like this:
<script> $(".rows").each(function(){$(this).remove()});</script>
Now what gets output is exactly what we wanted because we told the engine not to replace it.
Changelog
- 0.0.1 - 4/10/15
- Initialization
- 0.0.2 - 4/10/15
- Documentation update
- 0.0.3 - 4/11/15
- Added support for if/elseif/else
- 0.0.4 - 4/11/15
- Documentation update
- Enhancements
- Added extra npm tags
- 0.0.5 - 4/11/15
- Added support for literal's
- Added npm repository
- 0.0.6 - 4/11/15
- Added file include support
- Documentation update