WebUtil::StaticTemplateManager Class Reference

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...

Inheritance diagram for WebUtil::StaticTemplateManager:
[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< string, bool > | getTemplateHash ()
| returns a hash of template names, values are True or NOTHING if no templates are currently cached
list< string > | 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< HttpResponseInfo > | render (string tname, hash ctx, int code=200, *hash< auto > hdr)
| explicitly renders the given template with the given argument More...
| setupProgram (Program p)
| override in subclasses to customize the setup of template Programs; this method performs no action
*hash< HttpResponseInfo > | tryRender (string tname, hash ctx, int code=200, *hash< auto > hdr)
| explicitly renders the given template with the given argument context hash if the template exists; if not, returns NOTHING More...

## Protected Member Functions

Sequence | seq ()
| Sequence for template function names.

## Protected Attributes

int | po
| parse options for template containers
hash< string, hash< auto > > | th
| Maps template names to function names.
hash< string, TextTemplateBase > | toh
| Maps template names to TextTemplateBase objects.

## Additional Inherited Members

Static Public Member Functions inherited from WebUtil::StaticTextTemplateBase
static string | add (Qore::Program p, string fn, string name, string src)
| adds a template function to a template Program object More...
static string | getContentType (string name)
| returns the content type from the file name, ignores any leading "q" in the extensions, assuming a template
Public Attributes inherited from WebUtil::StaticTextTemplateBase
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
Static Protected Member Functions inherited from WebUtil::StaticTextTemplateBase
static bool | doBlock (string end, reference< string > src, string type, reference< int > i, reference< list< hash >> l)
| a helper method used in parsing
static string | getCode (bool bare_refs, string fmt)
| a helper method that strips dollar signs from code when PO_ALLOW_BARE_REFS is set in the template program

## Detailed Description

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:

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

## Member Function Documentation

## add()

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

adds a template to the object

Example:
  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)

## render()

hash<HttpResponseInfo> WebUtil::StaticTemplateManager::render | ( | string | tname,
|  | hash | ctx,
|  | int | code = `200`,
|  | *hash< auto > | hdr
| ) |  |

explicitly renders the given template with the given argument

Example:
  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

## tryRender()

*hash<HttpResponseInfo> WebUtil::StaticTemplateManager::tryRender | ( | string | tname,
|  | hash | ctx,
|  | int | code = `200`,
|  | *hash< auto > | hdr
| ) |  |

explicitly renders the given template with the given argument context hash if the template exists; if not, returns NOTHING

Example:
  *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)