Qore WebUtil Module Reference 1.6.2
Loading...
Searching...
No Matches
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...

#include <WebUtil.qm.dox.h>

Inheritance diagram for WebUtil::StaticTemplateManager:
[legend]

Public Member Methods

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

Private Member Methods

Sequence seq ()
 Sequence for template function names.
 

Private Attributes

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

Additional Inherited Members

- Static Public Member Methods 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 Private Member Methods 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:

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<auto> $h ($ctx.workflows.pairIterator()) { %}
<option value="{{ $h.key }}">{{ $h.value.label }}</option>
{% } %}
</select>
</fieldset>
</form>";
tm.add("/html/index.qhtml", qhtml);
Parameters
namethe name of the template
srcthe 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)
ctthe 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< auto >  ctx,
int  code = 200,
*hash< auto >  hdr 
)

explicitly renders the given template with the given argument

Example:
hash<HttpResponseInfo> h = tm.render("html/index.qhtml", ctx);
Parameters
tnamethe template name
ctxthe context argument for the template
codethe HTTP response code for the response, if not present then 200 "OK" is assumed
hdrany 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-ERRORthe given template does not exist

◆ tryRender()

*hash< HttpResponseInfo > WebUtil::StaticTemplateManager::tryRender ( string  tname,
hash< auto >  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<HttpResponseInfo> h = tm.tryRender("html/index.qhtml", ctx);
Parameters
tnamethe template name
ctxthe context argument for the template
codethe HTTP response code for the response, if not present then 200 "OK" is assumed
hdrany 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)