This is a simplistic, fast-parsing code-caching template system.
It takes a template, converts it into a perl subroutine and then runs the subroutine as
$code->($view,$req,\%args);
Practical upshot is, the CODE is cached, not the data, thereby making it possible to cache templates across multiple requests. On that note, one must be careful not to store data otherwise data will bleed across multiple requests.
Always use my() variables, don't create subroutines, etc..
Template syntax is really simple, asp/jsp like:
<%
$req->out()->header("Content-Type: text/plain");
my $foo = $req->param("FOO");
my $title = $args->{title};
%>
= is a way of placing an implicit $req->out()->print($text), that is,
it prints the result of the perl expression.
Hello: FOO is:<%= $foo; %>
<%-- This is a comment %>
Just comments, perl code and HTML.
Everything is run in te GenieMod::View::Template::Environ package name space, under the strict pragma, so, all variables MUST be declared.
Templates can (and are) shared among multiple requests, therefore it is usually a mistake to attempt to hold on to any data.
If you hold on to data, there is the possibility it will bleed across web pages, which is very much not desired if for example, credit card numbers are used.
Don't create subs or do anything fancy within a template, as these may create memory leaks.
Each template has the following variables available:
The template name.
The perl source code (what this template looks like after being parsed)
The View object.
The request object
The template arguments, either specified in the conf file in the args section, or, passed along via include()
In addition, you have the functions available from GenieMod::View::Template::Environ
See GenieMod::View::Template::Environ
These are the standard View methods.
Look in the path for $template_name, returns the filename or nothing. Use this if you wanted to dump a files contents, for example.
Sends template data out through request. (Does NOT cache)
These are available from within a template. They are not to be used on the object directly, but rather, from within the display() templates.
Like include() but uses no cache, uses no template path.
Include a template, with arguments. (So you can for example, set the title of a page you are including)
$view->include("header.ptml",title => "Header Title Here");
Important: do not call outside of a template The right way is via the display() method, which needs to set up the request and some tracking variables.
This fetches the args section from the conf file. In practice, it is the same as:
$v = $req->pad('View_Tpl_Args');
%v = %{$v}; # You get %v
With an argument, it'll return that specific view argument.
my $file = $view->varg('file');
Get the name of the view (the one the controller returned)
Get the template file of this view. (same as varg(file) actually)
A handy debug string to have in the footer of a document is something like this:
<%
$req->out()->printf('action:%s file:%s view:%s',
$req->action(),$view->viewfile(),$view->viewname()
);
%>
These aren't meant to be used inside templates, but may be useful of extending..
Load a template. Looks in the template path and fetches the raw template data.
Maintains an internal code reference cache of templates. These are compiled subroutines.
These are for parsed templates and are not used here. The speed improvement is minimal, they are provided as hooks if it's ever required.
parse template data.
returns a string that is fed to eval. Provided to allow over-riding, the returned value must be a perl string.
This is the package that templates are compiled in, it is not object oriented.
These functions are available to templates.
UrlEncodes a string, so that it can be used as a URL, something like "Hello World" would be converted to "Hello%20World".
This escapes the HTML characters &, < and >
These aren't meant to be run inside of templates.
Compiles code in $string. returns a code reference that can be run with the following arguments:
$code->($view, $req, $out,\%args)
In addition, the variable $source and $tmpl are available as well. $source is the template in perl source form, and $tmpl is the template ID.
Any subroutines in this package are available to the compiled code.
Note: this code is meant to be cached! Assigning to $tmpl or $source is not advisable.