TintJS
Tint uses templates which specify an interface of variables, namespaces, and functions. That interface is used to compile a prototype for objects which build the output.
The template:
<div id="container"> <div id="nav"> $nav{ <ul class="nav-list"> $item() { $header(label){<li class="nav-header">$label;</li>}header; $link(label, uri){<li><a href="$uri;">$label;</a></li>}link; }item; </ul> }nav; </div> <div id="content"> <h3>$title;</h3> <table> $message(uri, author, title, date) { <tr> <td>$author;</td> <td><a href="$uri;">$title;</a></td> <td>$date;</td> </tr> }message; </table> </div></div>
The javascript:
// Compile the prototype// `templateString` contains the above templatevar Tmpl = Tint; // ... // Instantiate the builder objectvar tmpl = ; // navtmplnav;tmplnavlink'Inbox' '/';tmplnavlink'Settings' '/settings';tmplnav;tmplnavlink'MyService' '/myservice'; // contenttmpltitle = "Your Inbox";for var i=0; i < messageslength; i++ var message = messagesi; tmpl; // Generate outputvar html = tmpl;
You can also extend the prototype, if you like:
// Add a custom constructorvar Tmpl = templateString { thisnav; thisnavlink'Inbox' ''; thisnavlink'Settings' '/settings'; thisnav; for var i=0; i < serviceslength; i++ thisnavlinkservicesiname servicesiuri; };Tmplprototype { this;}; // ... // Instantiate the builder objectvar tmpl = my_services;tmpltitle = "Your Inbox";for var i=0; i < messageslength; i++ tmpl;var html = tmpl;
How it works
Tint's templates use 3 different constructs:
- Blocks
- Variables
- Functions
Blocks are just namespaces for parts of the template. They don't change the output.
$block_name{ whatever }block_name;
Variables are direct substitutions. They're replaced with whatever value they're assigned.
$variable_name;
Functions are blocks which can be added multiple times, and which take parameter lists.
$func_name(param1, param2) { whatever $param1; whatever $param2; }func_name;
That's it; those building blocks are enough to generate your output. Any logic you need (like conditionals, escaping, or lists with commas on all but the last item) is added to the prototype, by you, using a language that's designed for it (Javascript).
A few additional notes
Function calls generate blocks which are stored in an array with a prefix added to the name. That is, if my template has a $profile(username)
function, then I can access the blocks it creates at _profile[]
. Function parameters are stored as variables with the same name as the parameter itself: _profile[0].username
.