Tmplz Template API

Tmplz Tags

Extra Tags: AttrSection

This tag is only useful when writing HTML (or XHTML). It provides a workaround to a minor quirk where raw templates can fail HTML validation.

Background

Suppose one wants to create a generic template for checkboxes. One way to write it would be:

  <input type="checkbox" checked="[$Slot OnOff]" name="Elephant" value="yes"/> 

This is "wrong", however, because standards dictate that the only valid value for checked is "checked"; if it is to be "unchecked", the checked attribute should be left off (ditto for the option element's selected attribute). The following is better:

  <input type="checkbox" [$Section checked]checked="checked"[$Section] name="Elephant" value="yes"/> 

We placed a Section around the checked attribute because Sections are invisible unless they are explicitly shown. This way one can use the same template for both checked & unchecked checkboxes. Unfortunately, the "raw" (unparsed) template will be flagged as illegal by an HTML validator because it looks like an invalid attribute.

Solution

Enter the AttrSection tag:

  [$AttrSection checked]
  <input type="checkbox" checked="checked" name="Elephant" value="yes"/> 

This accomplishes the exact same thing as the second example above, using valid HTML: The checked attribute is wrapped in a Section named "checked". Note that the attribute is required to have a value, surrounded by double quotes (here, "checked").

Variations

The Section name doesn't have to be the same name as the attribute. This creates a Section named "WeHaveElephants" which wraps the checked attribute:

  [$AttrSection WeHaveElephants checked]
  <input type="checkbox" checked="checked" name="Elephant" value="yes"/> 

Also, as should be expected, the attribute itself can contain anything a Section can contain:

  [$AttrSection class]
  <div class="[$Section Default]default[$Section] [$Slot Additional]"/> 

This is the end of the Tmplz Tags documentation.