Learn the ZPT syntax
ZPT-Sharp borrows its syntax from Zope Page Templates (hence ZPT). It has no dependency upon Python or the Zope framework though; ZPT-Sharp is a complete standalone implementation of the ZPT syntax for .NET & Mono.
ZPT is an attribute language
<p>Hello, my name is <strong tal:content="name">Sally Smith</strong>.</p>
As the simple example illustrates, the ZPT syntax which transforms the document is written using HTML/XML attributes.
Unused source content is discarded
Let's look again at the example above; we will assume that the name variable contains the value Joseph Bloggs. The resultant rendering would look like this:
<p>Hello, my name is <strong>Joseph Bloggs</strong>.</p>
Both the tal:content
attribute and the text Sally Smith are discarded by the
ZPT rendering process.
METAL attributes let you reuse markup
The first step in the ZPT-Sharp rendering process is that all metal:
attributes are fully resolved
and expanded.
METAL stands for Macro Extension Template Attribute Language; it allows you to
define reusable trees of markup within macros.
When those macros are used, their markup is inserted at the point of usage;
slots allow for customisation.
Macros can be defined in the same document as they are used but they definitely don't have to be. Usually macros will be placed in shared view files, referenced by the documents which use them.
An example of METAL syntax at work
This example helps demonstrate METAL's purpose. You may refer to the METAL reference to learn more about the functionality METAL offers and its syntax.
<!-- Here is the macro code -->
<article metal:define-macro="standard_article">
<header>
<h2 metal:define-slot="title">Article title</h2>
</header>
<div metal:define-slot="body">
Article body
</div>
<footer>
<p class="disclaimer">Standard disclaimer</p>
</footer>
</article>
<!-- Here is some markup which uses that macro -->
<article metal:use-macro="standard_article">
<h3 metal:fill-slot="title">Macros are great</h3>
<div metal:fill-slot="body">
<p>
This content will fill the <strong>body</strong> slot.
</p>
</div>
</article>
<!-- Here is how this would look once rendered by ZPT-Sharp -->
<article>
<header>
<h3>Macros are great</h3>
</header>
<div>
<p>
This content will fill the <strong>body</strong> slot.
</p>
</div>
<footer>
<p class="disclaimer">Standard disclaimer</p>
</footer>
</article>
TAL attributes bind your model data
After all of the METAL attributes have been processed, the next step is to bind an object model you have
provided into the document.
The syntax for this is TAL - Template Attribute Language.
tal:
attributes may repeat sections of markup,
set HTML/XML attributes and their values,
conditionally determine whether elements and their contents are rendered,
provide content and more.
An example of TAL syntax at work
Don't worry if you do not immediately understand everything which occurs in this example; its purpose is to show what can be done in a very small amount of markup. To learn more of its functionality and syntax, please refer to the TAL reference.
<!-- Here is some source markup -->
<h2>Fan club members</h2>
<ul>
<li tal:repeat="member here/MemberList"
class="activity_Unknown"
tal:attributes="class string:activity_${member/ActiveStatus}">
<a href="mailto:email@example.com"
tal:content="member/FullName"
tal:attributes="href string:mailto:${member/EmailAddress}"
tal:omit-tag="member/IsEmailHidden">Member name</a>
</li>
</ul>
<!-- Here is how that source would render if "MemberList" contains two members:
One inactive: "Jane Doe" with her email address hidden
One active: "Sammy Sullivan" with his email address shown -->
<h2>Fan club members</h2>
<ul>
<li class="activity_Inactive">
Jane Doe
</li>
<li class="activity_Active">
<a href="mailto:sammy.s@mailbox.net">Sammy Sullivan</a>
</li>
</ul>
Access your data with TALES
Complementing METAL & TAL is TALES: Template Attribute Language Expression Syntax. TALES does have any attributes; it is used within ZPT attributes and provides the syntax for referencing your object model.
TALES supports a number of expression types out of the box - and it is extensible via plugins. The recommended default expression type is the path expression. Path expressions provide a syntax which looks similar to a URL for navigating an object graph. It simplifies object access, method invocation and .NET object indexers.
Some TALES examples
In each of the following examples, a C♯ expression is compared with a TALES equivalent. These and other expression types are explored in detail within the TALES reference section of this website.
-
The C♯ expression
peopleList[5].GetPets().Count
could be represented by the path expressionpeopleList/5/GetPets/Count
-
String expressions permit verbatim strings and insertion of sub-expressions.
The TALES expression
string:My name is $name
is equivalent to the C♯ 6 interpolated string expression$"My name is {name}"
. -
CSharp expressions permit the evaluation of native C♯ expressions should they be required.
The TALES expression
csharp:myDateTime.ToString("MMMM")
would output a month-name. Note the HTML-encoding of the quotation marks; because TALES expressions are written inside HTML/XML attributes, naked double-quotes are not permitted.