Tmplz Template API

Tmplz Tags

Extra Tags: TagWith

The TagWith tag allows a template author to change the delimiter characters for Tmplz tags. When the parser encounters a TagWith tag, it switches to the specified syntax for all tags thereafter. A file can have multiple TagWith's.

Example 1

  [$TagWith {{ }}]
  {{Slot X1}}
  
  {{TagWith "{$" "}"}}
  {$Slot X2}

First we changed delimiters from the default [$...] to {{...}}. Then we changed them again, this time to {$...}, and using quote (") characters around the new delimiters to keep the parser from getting confused. Our end result is two Slots: X1 and X2.

Example 2

  [$TagWith javascript]

This sets the delimiters to /*...*/ (Javascript comment syntax), and it's one of the "named" TagWith options. Here are all of them (they are case-insensitive, presented here in lower case):

NameResult
default[$ ]
sql/* */
javascript/* */
css/* */
curlies{ }
brackets[ ]
xml<-- -->
html<-- -->
revertReturns to whatever was in effect
before the most recent TagWith.

Example 3

  ~~TagWith javascript
  /*Slot Bar*/

  ~~TagWith {{,}}
  {{Slot Bar}}

Here we are using a somewhat atypical syntax, but it works no matter what the current tag delimiters are: Start with ~~TagWith, followed by a single space, followed by our delimiter definition, and end with a blank space or line break. The delimiter definition can use one of two patterns:

  • A "named" delimiter set, as is the case with javascript above;
  • A pair of delimiters separated by a comma, without any quotes or spaces, as with {{,}} above.

This can be helpful in a situation where one has no guarantee about the default delimiters, such as when writing reuseable template libraries (also refer to "Setting Defaults" below).

Additional Notes

Delimiters Don't Have to be Weird

If "{" and "}" are the current delimiters, and one puts {BlahBlahBlah} in a template, "BlahBlahBlah" is not a Tmplz tag, so this will be treated as normal text. Thus there is no need to use excessively weird delimiters in an effort to avoid accidentally confusing the template parser.

Setting defaults

Instead of typing TagWith into every template, the default tag delimiters can be set by a Java programmer using TemplateManager.setTagDelimiters().

XML

Tmplz does not have any way to specify XML-compliant tagging, e.g.:

  <Section Foo> blar blar blar </Section>
  <Slot Bar/>

Next page: Trim