jstl - JavaScript as a Templating Language
jstl
is a templating library for JavaScript, using JavaScript.
Rather than trying to design a new language, jstl
allows you to write JavaScript code for the control logic of your templates, while also providing a customisable "short form" for variable substitution.
jstl
takes in a template, and produces a function. This function includes JavaScript code from the template, so it is basically equivalent to eval()
- DO NOT RUN TEMPLATES YOU DO NOT TRUST.
Example code:
<?jsfullname?> <?js if (window.location.substring(0, 5) == "https") { ?> Encrypted <?js } else { ?> Unencrypted <?js } ?>
Everywhere <?js
is used, <%
can be used instead (and %>
can be used in place of ?>
).
Another templating library?
Yeah, I know there are already quite a few. But they generally suffer from the following problems:
- New syntax - the more complex ones define their own syntax for
if
/foreach
/etc. - Not customisable - they give you a set of pre-defined output filters (HTML entities, URI encoding, etc).
jstl
tries to avoid these, by piggybacking on the JavaScript language while providing a simple base for extending behaviour.
Writing templates
There are two different ways to use the special tags in a template.
<?jsvariable?>
or <%variable%>
1) Simple substitution:If there is no space following the <?js
the block is substituted with a value. By default, this is taken as a property of the first function argument (HTML-escaped).
Example template:
<%name%> likes <%food%>.
Calling the function:
var obj = name: "Everybody" food: "chocolate";var html = ;
Result:
Everybody likes chocolate.
<?js ... ?>
or <% ... %>
2) JavaScript code: If there is whitespace following the opening <?js
/<%
, the block is run as JS code.
The special function echo()
is defined, which contribues to the output of the function.
Example template:
Current URI: <?js echo(window.location.toString()); ?>
Result:
Current URI: http://example.com/
Loading templates
Templates can be loaded either as strings (jstl.create(str)
) or they can be loaded from C-style comments in your JS file, assuming that file is accessible via AJAX.
The syntax for loading from comments is to start your comment /* Template: <identifier>
followed by a newline. To load all templates in a file, call jstl.loadTemplates()
from that file.
Example:
template = jstl; /* Template: example-template<div> <?jstitle?></div>*/template2 = jstl;
Customising
Behaviour can be modified either by passing arguments into template.compile()
, or by changing properties in the jstl
object.
The "no spaces" behaviour
You can completely customise the behaviour of the "no spaces" form.
The way you do this is by passing in a function to t.compile()
of the following form:
t;
You might need to look at that twice - you supply a compile-time function which returns a function that is called at render time.
For example, a simple example might be:
var t = jstl;var f = t; var value = name: "Mary";var result = ; // returns: "Name: Mary"
The default function (that t.compile()
uses if you don't supply your own) is available at jstl.defaultFunction
. It looks very much like the above, except it HTML entity-encodes the output.
You can change this default by altering jstl.defaultFunction
.
"Header code"
You can also provide some JavaScript code to sit at the top of the compiled template. It's completely equivalent to adding the same code to the top of the template.
var f = t;
A good use for this might be to alias function arguments (as in the example) - that makes the value
variable available for use within your templates, so you don't have to pepper your code with arguments[N]
.
The default "header code" is actually the one from above example - however, you can change this by altering jstl.defaultHeaderCode
.
Wait - how does it get the templates from JavaScript comments?
In-browser
The secret is that when jstl.getTemplate()
is called, it calls through to jstl.loadTemplates()
. This function figures out what source file is running (it should be the last <script>
in the document), fetches it via AJAX and inspects it for comments.
If for some reason you aren't calling jstl.getTemplate()
when the script loads (I can't think why you would), then you should call jstl.loadTemplates()
manually, optionally including the URI of the source file containing templates.
Using Node
Since the above AJAX trick doesn't work when running server-side, every script containing templates should call jstl.loadTemplates(__filename)
, so the library can read the file and parse all templates from it.