terp template syntax

< Template Introduction Metadata >

Template Syntax

Terp's template syntax is relatively straightforward, but there are some advanced features that require a more detailed explanation. In general, terp templates consist of text with embedded statements that follow the form

   embedded:    ${ exprOrStmt metadata? 
                   | metadata 
   metadata:    ( # id [ = expr ] )+

One of the simplest useful embedded statements is ${now}, which expands to the current time in ctime-format. now is a built-in variable identifier. It gets somewhat more complicated when you take metadata into account. Metadata can accompany any embedded statement to provide

  • local scope variables or
  • formatting switches

Let's look at an example. The following file header comment was generated dynamically from the fileheader.tpl template. The first two lines in this example have only been inserted to provide a visual alignment cue.

0         1         2         3         4         
 *                                               *
 * Build-Host: x2100                             *
 * Build-Time: Mon May 23 14:35:43 EDT 2016      *
 * Build-User: unknown                           *
 *                                               *

This is the template that created the above header.

${{0,1,2,3,4}["%d         "]}
${ 5 * "0123456789" }
${#width=50}/${'*' * (width-indent-1)}
 * Build-Host: ${localhost.name}${#right}*${end}
 * Build-Time: ${now}${#right}*${end}
 * Build-User: ${localhost.env['username']!=null?localhost.env['username']:(localhost.env['user']!=null?localhost.env['user']:"unknown")}${#right}*${end}
 ${ '*' * (width-indent-2)}/${end}

This template looks a bit convoluted, but it's really quite simple.

As already mentioned, the first two lines provide a "ruler" so you can see the effects of changes in line width or indentation level.

A command which sets the width of the page (every individual line in the scope of the statement) follows. The width is only relevant when you use alignments other than left or when you use automatic linebreaks for long lines. We have to set the desired width because we want to have terminating asterisks ans we will use right aligned text for that purpose. You can change the width as often as you want, for example to create this kind of boxed comment and then switch to a different width for the text that follows.

In this case, we want to fill the the first line with asterisks, so we use a a calculated number of * characters. If you always use a width of 50, you could of course have hard-coded the number 49.

Every following line starts out with a (default) left aligned  * string, followed by some variable text, terminated by a right-aligned asterisk which marks the end of the line. By the way, if you're interested in seeing a user name other than "unknown," simply define the user.name variable before performing the expansion.

The final line is again using a calculated number of asterisks followed by a terminating slash.

Copyright 2006-2016 by Codemesh, Inc., ALL RIGHTS RESERVED

terp template statements
codemesh.com home expressions templates ant about us contact us download