HTML Templates
Sometimes it's convenient to format your application's data as an HTML document. The scriptparser class can help you create simple templates that combine your data with minimal logic flow into HTML. The scripting commands that come with this templating engine are minimalistic, their intention is to be more about structured data formatting than serious control flow.
Using Sections
Looping Through Arrays
Conditional Evaluation
Assigning and Modifying Variables
Using scriptparser
The scriptparser class turns your HTML template into a syntax tree, which you later evaluate through the run method:
#include <grace/scriptparser.h> int myApp::main (void) { value data = gatherData(); scriptparser script; string out; script.build (fs.load ("tmpl:data.thtml")); script.run (data, out); fout.puts (out); return 0; }
A template page consists of one or more sections indicated by the @section command. The parameter data passed to scriptparser::run is available by surrounding the variable key by dollar signs:
@section main
<html>
<head>
<title>Information about $firstname$ $lastname$</title>
</head>
<body>
First name: $firstname$<br/>
Last name: $lastname$<br/>
</body>
</html>
Using Sections
The @section command allows you to create 'subroutines' inside your template. You can reference other templates by putting it in as a double-bracketed html tag <<likethis>>. Any attributes included will be stored into the script environment variables:
@section page
<html>
<head>
<title>$title$</title>
</head>
<body>
<div class="content">
@section /page
</div>
</body>
</html>
@section main
<<page title="Hello, world">>
Hi, world.
<</page>>
This allows you to express more complex markup as mere metatags, making the page structure more obvious.
Looping Through Arrays
Sometimes it's convenient to represent a list of objects in the HTML output. To make this possible, scriptparser has the @loop and @endloop commands. Given an array like this:
value env = $("products", $("STPX001", $("name", "Storpel") -> $("price", 14.95) )-> $("ARRW001", $("name", "Arrow") -> $("price", 4.95) )-> $("USANRO1", $("name", "Flag pin") -> $("price", 1.95) ); script.run (env, out);
and a template like this:
@section main
<<page title="products">>
<table border="0">
@loop products
<tr>
<td><a href="$@$.html">$name$</a></td>
</tr>
@endloop
</table>
<</page>>
the template will go through all the entries in the env["products"] dictionary. The $@$ variable contains the current index of the node being inspected inside the loop. Please note that the scriptparser doesn't distinguish variable scope, so namespace clashes can happen between inner-loop variables and declarations outside the loop.
Conditional Evaluation
The @if command is scriptparser's primitive implementation of conditional parsing. Its implementation is about as sophisticated as that of a typical shell environment: Two values can be compared with a specific operator. The values are expanded from variables:
@if $#count$ == 0
count is 0.
@endif
@if $#counter$ > 8
counter > 8
@endif
@if "$user$" == ""
no user found
@endif
@if "$user$" != "root"
not allowed
@endif
Assigning and Modifying Variables
The @set command allows you to set or change the contents of a variable. Like @if, it's a basic left side, operator, right side deal with no further expression evaluation. You have the following options:
@set name = "John"
@set count = 0
@set count += 1
@set count *= 4
@set name ~= "s/John/Mike/"
@set name += " Hunt"
@set thevar = "name"
@set $thevar$ = "John Doe"
| Previous Chapter | Table of Contents | Next Chapter |