CompileTime vs. RunTime
WACT handles templates in two stages; a compiling stage (Compile Time) and an execution stage (Run Time) (see also WACT Template Component Architecture).
During the compiling stage, your template is parsed and a Compile Time Component Tree is constructed to represent it. In Compile Time Component Tree are objects (subclasses from Compiler Component) which write output into the compiled template. The compiled template is your WACT template compiled into a native PHP script, ready for execution.
During the execution stage, the compiled template is invoked and a lightweight (fast) runtime component tree is built (each component in the tree being a subclass of Component), which you have access to via the Template API, allowing you to control the runtime components.
Because the compiling stage is expensive, in terms of performance, you can disable it with the config.ini setting FORCECOMPILE = FALSE. Normally you will only use the compile stage when you are developing an application and the template is being modified. Once the application is “live”, the compiling stage should be disabled (the template is not changing), meaning only the fast, Run Time stage takes place.
Tag Types
There are four flavours of tag in WACT (see also Supported Tags and Define Tag Information);
Silent Compiler Directive Tags
Silent compiler directive tags provide instructions to the template engine, influencing template compile-time behaviour but without writing anything to the compiled template.
They also do not have a runtime component. That means when you load the template from your PHP script, via the Template class, there is no opportunity to interact with them - they do not exist at runtime and have no entry in the runtime component tree (you cannot find them with the Template::getChild() method).
They are the least common type of tag in WACT although examples include the Core Set Tag (used to set the value of a variable reference so the value gets written into the compiled template);
<core:set varname="value"/>
and the Core Import Tag (used to import a set of values to be written into the compiled template):
<core:import file="menu.vars"/>
Compiler Directive Tags
Like silent compiler directive tags, compiler directive tags do not have runtime components, but they do write into the compiled template.
Examples include the Core Include Tag (used to include one template in another, much like the PHP include function):
<core:include file="header.html"/>
Core Optional Tag and Core Default tags (used for an if/else type condition in the template, depending whether the variable names in the for attribute has been set in the DataSpace);
<core:optional for="authenticated">You are logged in</core:optional>
<core:default for="authenticated">You are not logged in</core:default>
Server Component Tags
Server component tags do have a corresponding runtime component which you can interact with from your PHP script, via the Template API and they write into the compiled template.
They are the most common type of tag in WACT, examples include the Core Block Tag (marks a section of the template which can be hidden or displayed dynamically at runtime):
<core:block hidden="true">This is invisible</core:block>
and the Calendar Month Tag (used to build a navigable calendar for a month)
<calendar:month/>
Server tag component tags are a powerful feature in WACTs template engine, for example:
- <page:navigator /> tag generates navigation for paged result sets and can easily be attached to a <list:list /> tag.
- <calendar:month /> tag builds a navigable HTML table to represent a calendar month, without you needing to worry about the HTML or the math working behind the scenes that calculates the calendar.
Server Data Component Tags
The Server Data Component Tag is an extension of the Server Component Tag and deals with connecting a WACT Data Source to a tag. Otherwise the same rules apply as with the Server Component Tag - there is corresponding runtime component and it writes into the compiled template.
Some examples of Server Data Component Tags are:
- <list:list /> tag makes it very easy to loop through a Record Set and embed it in HTML.
- <data:table /> tag allows you to generate instant HTML tables for a Record Set (without typing a single <td> tag)
Server Tag Component Tags
Server tag component tags are an extension of server component tags (they have a runtime component and they write into the compiled template) but the important difference is they also correspond to native tag names in the XML vocabulary you are using. For example the Select Tag corresponds to the HTML <select /> tag.
In other words server tag component tags allow behaviour to be added directly to an HTML tag in the template, on the server side, before the tags are sent to the browser. There are some special rules that need to be considered when dealing with Server Tag Component Tags, such as how to identify them to the template engine, which are explained below.
Widgets
Widgets are components that exist purely at Run Time and allow the output to be manipulated dynamically outside of the template engine. The template engine is unaware of their existence.
They are recommended only for special cases where additional ability to manipulate the output is required. Because they are generated at Run Time they have a greater overhead than Compile Time components.
Examples include Text Widget (used to insert plain text into the body of a Generic Container Tag), Tag Widget (used to add tag into the body of a Generic Container Tag, but the tag cannot have further widgets added to it (e.g. a <br />)) and Tag Container Widget (used to add tag with can have further widgets added to, into the body of a Generic Container Tag (e.g. a <div></div>)).
Tag Type Summary
| Tag type | Writes to Compiled Template? | Runtime Component? | Matches Real HTML Tags? |
|---|---|---|---|
| Silent Compiler Directive Tag | No | No | No |
| Compiler Directive Tag | Yes | No | No |
| Server Component Tag | Yes | Yes | No |
| Server Data Component Tag | Yes | Yes | No |
| Server Tag Component Tag | Yes | Yes | Yes |
| Widgets | No | Yes | Yes (your choice) |
Expessions and Variable References
A Variable Reference is a placeholder to be filled by the value PHP variable at Run Time. It is a form of template expression in WACT. For a more detailed description of expressions, see the Output Expressions.
The basic syntax is:
<h1>{$Title}</h1>
At runtime WACT will look for the a variable called 'Title' inside the template Data Space where the Variable Reference was declared. To populate the above variable from your PHP script you might have:
$Page = & new Template('/index.html');
// Set the variable reference value
$Page->set('Title', 'This is the title of the page');
Variable references can be used for attributes as well, even for the value of an attibute in a WACT tag (but not in the ID attribute of a WACT tag - see below).
A special case for Variable References is the Core Data Space tag, which defines an internal scope for data inside the template. If you are using the CoreDataSpace tag, you can access the root Data Space (the template Data Space) by replacing the $ context definiation in the variable reference with a # character. You access the parent Data Space using a ^ character to the $ context definition e.g {$#variableName} or {$^variableName}.
Note: There is further syntax available which extends Variable References, described in the Output Expressions
Note: you can also populate a variable reference at Compile Time, using the Core Set Tag.
How does WACT identify Server Tag Component Tags?
To tell the WACT template engine that a native HTML tag should be made into Server Tag Component Tag, the attribute runat must be added to the tag in the template and given the value “server” for example;
<!-- This form is just plain HTML -->
<form id="MyFormA">
<!-- ... -->
</form>
<!-- This form is a Server Tag Component Tag because it has the runat="server" attribute -->
<form id="MyFormB" runat="server">
<!-- ... -->
</form>
Only the second form above will become a Server Tag Component Tag, the first being treated as plain HTML.
Template Error Reporting
The template engine is “self aware” in that it knows how to spot violations of (most of) the syntax rules of WACTs markup. For example if you assign the same ID to more than one WACT tag (plain HTML tags ignored), you will get a duplicate ID error as well as the file and line number where the duplicate tag was found. Different rules apply to different tags but another common error will be failing to close a tag which must be closed.
Some syntax errors will be harder to track down - WACT can only report the error at the point where it found it but you should at least know in which template file the error occured and what type of error you are looking for.
Template Debugging
There are three tags currently available to help with debugging templates;
- DevSourceTag - allows you to view a section of the compiled template file (this is mainly for tag development)
- DevTreeTag - allows you to view the compile time component tree so you can see which tags have been registered (again mainly for tag development)
- DevDataSpaceTag - allows you to see the variables residing in the dataspace where this tag was placed. Useful for examining the contents of a form component, for example.
More such functionality is planned, such as tracing and profiling.
One further tag, along this lines, is the DataDumpTag, which allows you to dump a RecordSet (e.g. database query result or ArrayDataSet) into the template output.
Known Template Quirks
Generally the template engine is fairly robust and stable although there are some outstanding issues. For the most up to date information, see the Template Component bug list as well as the “Template Group” unit tests.
- Badly formed HTML - some instances of badly formed HTML will trip the parser (XML_HTMLSax in fact), no error message being generated while wierd output is produced.
- Self defence - run your templates through an HTML validator. See the Template Group unit tests for details.
- Variable references in the id attribute of WACT components. Varying wierd behaviour depending on the tag. ←- I think variable references in “compile-time attributes” are not (yet?) supported.