A container for holding static text templates (ie that do not change once created in the template manager) with mixed text and Qore code.
More...
|
string | add (string name, string src, *string ct) |
| adds a template to the object More...
|
|
| constructor (int po=DefaultProgramOptions) |
| sets up the object
|
|
*hash | getTemplateHash () |
| returns a hash of template names, values are True or NOTHING if no templates are currently cached
|
|
list | getTemplateList () |
| returns a list of template names, an empty list is returned if there are no templates
|
|
bool | hasTemplate (string tname) |
| returns True if the given template exists, False if not
|
|
hash | render (string tname, hash ctx, int code=200, *hash hdr) |
| explicitly renders the given template with the given argument More...
|
|
*hash | tryRender (string tname, hash ctx, int code=200, *hash hdr) |
| explicitly renders the given template with the given argument context hash if the template exists; if not, returns NOTHING More...
|
|
| constructor (int po=DefaultProgramOptions) |
| sets up the object
|
|
|
Sequence | seq () |
| Sequence for template function names.
|
|
|
hash | th |
| Maps template names to function names.
|
|
A container for holding static text templates (ie that do not change once created in the template manager) with mixed text and Qore code.
To give the template programs a specific imported API, subclass this object and use the following methods to import an API into the template Program object in your subclass's constructor:
adds a template to the object
- Example:
my string $qhtml = "<form id=\"upload_form\" enctype=\"multipart/form-data\" method=\"post\" action=\"{{ $URL_PREFIX }}upload-file">
<fieldset class=\"workflow_list\">
<legend>Step 1: Select Workflow</legend>
<select class=\"worfklows\" name=\"workflow\" id=\"workflow\">
{% foreach my hash $h ($ctx.workflows.pairIterator()) { %}
<option value="{{ $h.key }}">{{ $h.value.label }}</option>
{% } %}
</select>
</fieldset>
</form>";
$tm.add("/html/index.qhtml", $qhtml);
- Parameters
-
name | the name of the template |
src | the source of the template; Qore source code is delimited as follows:
- expressions: Qore expressions are delimited by double curly brackets:
{{ }} ; such expressions are expected to return a string value that will be inserted directly into the text generated by the template
- statements: Qore statements are delimited by a curly bracket and percent sign:
{% %} ; these statements can be any Qore code legal for the template Program object; for example loop constructs are often used (see the example above)
|
ct | the Content-Type of the rendered output; if not given then the content-type is derived from the extension in the name if possible; if the content-type cannot be derived from the extension, then content-type "text/plain" is assumed |
The $ctx hash variable is always present in the template code; this is the call context variable, and is set to the same value as the cx argument in HttpServer::AbstractHttpRequestHandler::handleRequest() plus any context added by the actual handler handing the request. In the example above, a "workflows"
key assigned to a hash has been added to the call context variable.
- Returns
- the source code of the generated template (without Program overhead)
explicitly renders the given template with the given argument
- Example:
my
hash $h = $tm.render(
"html/index.qhtml", $ctx);
- Parameters
-
tname | the template name |
ctx | the context argument for the template |
code | the HTTP response code for the response, if not present then 200 "OK" is assumed |
hdr | any optional headers for the response (the "Content-Type" header is set from the templates "Content-Type" value automatically) |
- Returns
- a hash with the following keys:
code:
the HTTP response code corresponding to the code argument
body:
the rendered template
hdr:
a hash of headers corresponding to the hdr argument plus the "Content-Type"
key set from the template's "Content-Type" value)
- Exceptions
-
TEMPLATE-ERROR | the given template does not exist |
explicitly renders the given template with the given argument context hash if the template exists; if not, returns NOTHING
- Example:
my *
hash $h = $tm.tryRender(
"html/index.qhtml", $ctx);
- Parameters
-
tname | the template name |
ctx | the context argument for the template |
code | the HTTP response code for the response, if not present then 200 "OK" is assumed |
hdr | any optional headers for the response (the "Content-Type" header is set from the templates "Content-Type" value automatically) |
- Returns
- NOTHING if the template does not exist, otherwise a hash with the following keys:
code:
the HTTP response code corresponding to the code argument
body:
the rendered template
hdr:
a hash of headers corresponding to the hdr argument plus the "Content-Type"
key set from the template's "Content-Type" value)