WeBWorK::Template - apply a template to a ContentGenerator.
use WeBWorK::Template qw/template/; my $templateFile = "default.template"; my $cg = WeBWorK::ContentGenerator::SomeSubclass->new($r); template($templateFile, $cg);
WeBWorK uses templates to customize the presentation of pages. A template is a complete HTML document, containing normal HTML code and special escape sequences.
Escape sequences have a format similar to that of server-side includes (SSI). The format is as follows:
<!--#NAME ARG1="VALUE1" ARG2="VALUE2" ...-->
An escape's NAME and arguments (ARG1, ARG2, etc.) are case-sensitive,
the argument values may or may not be depending on the particular escape. Most
escapes have case-sensitive values.
Escape equences are replaced by dynamically generated content from WeBWorK's
content generation system, WeBWorK::ContentGenerator. When a template escape
NAME is encountered in the document, the template processor checks for a
method of the same name in the current content generator. If found, that method
is invoked as follows:
@result = $contentGenerator->NAME(\%escapeArguments);
where %escapeArguments contains the key/value pairs of arguments in the escape
sequence (like ARG1="VALUE1" ARG2="VALUE2"). The method may print() output
directly to the client or return a result. If the method returns a non-empty
value, it is sent to the client.
In addition to the normal escape sequences above, the escape sequences #if,
#else, and #endif are reserved and used to conditionally include portions
of the template in the output.
The #if escape sequence has the following form:
<!--#if PRED1="VALUE1" PRED2="VALUE2" ...->
When an #if escape is evaluated, each predicate (PRED1, PRED2, etc.) is
evaluated by calling a method named if_PRED in the current content generator
with the predicate's value as the sole argument. If no such method exists, the
predicate is false. If the method returns a true value, the predicate is true.
If any predicate is true, the code between the #if escape and a matching
#else or #endif escape is included in the output. #if statements can be
nested.
For example:
<!--#if loggedin="1"-->
<!--#if can="loginstatus"-->
<div class="LoginStatus">
<!--#loginstatus-->
</div>
<!--#endif-->
<!--#endif-->
<!--#if can="path"-->
<div class="Path">
<!--#path style="text" image="/webwork2_files/images/right_arrow.png" text=" > "-->
</div>
<!--#endif-->
Several predicate functions are defined in WeBWorK::ContentGenerator.
Process the template file $templatePath. Methods from $cg, an instance of WeBWorK::ContentGenerator, are called to handle escape sequences in the template.