Qore WebUtil Module Reference 1.0

- Main Page
- Namespaces
- Classes

- Class List
- Class Index
- Class Hierarchy
- Class Members

All Classes Namespaces Functions Variables Groups Pages
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
WebUtil::TemplateManager Class Reference

A container for holding text templates with mixed text and Qore code. More...

Inheritance diagram for WebUtil::TemplateManager:
[legend]

## Public Member Functions
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...

## Public Attributes
const | DefaultProgramOptions = PO_REQUIRE_OUR|PO_NO_TOP_LEVEL_STATEMENTS|PO_NO_INHERIT_USER_FUNC_VARIANTS|PO_NO_INHERIT_GLOBAL_VARS|PO_NO_USER_CLASSES|PO_NO_TERMINAL_IO
| default parse options for template Programs

## Private Member Functions
Sequence | seq ()
| Sequence for template function names.

## Private Attributes
Program | p
| Holds the template generation functions.
hash | th
| Maps template names to function names.

## Detailed Description

A container for holding text templates 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:

- Qore::Program::importClass()
- Qore::Program::importFunction()
- Qore::Program::importGlobalVariable()

## Member Function Documentation

string WebUtil::TemplateManager::add | ( | string | name,
|  | string | src,
|  | *string | ct
| ) |  |

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)

hash WebUtil::TemplateManager::render | ( | string | tname,
|  | hash | ctx,
|  | int | code = `200`,
|  | *hash | hdr
| ) |  |

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

*hash WebUtil::TemplateManager::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

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)