Invocation From Perl
This chapter describes, how you can use the Imperia view template processor independently.
General Usage#
The Imperia view processor is invoked from Perl by creating an instance of the Imperia::View::Parser class:
#! /usr/local/bin/perl
use strict;
use Imperia::View::Parser;
use CGI;
my $parser = Imperia::View::Parser->new(directories =>
['./myskins', '/etc/mytemplates']);
my $data = {
title => 'My first page',
cgi => CGI->new,
http_env => \%ENV,
};
print “Content-Type: text/html; charset=utf-8\n\n”;
my $view = $parser->parse('mytemplate.html');
print $view->render($data);
The only argument you can pass to the constructor is the search path for templates. This should be a list reference. Without a search path, only the current directory is searched. The method parse() takes the name of the template to parse, and returns an Imperia::View object.
Public Methods#
disallowPlugIns(PLUG_IN[,PLUG_INS...])#
You can pass a list of plug-in names that are not allowed in this parser instance.
pushImportRealm(PLUG_IN[, FUNCTION...])#
Loads the ViewImport
plug-in REALM
, and registers all functions passed in the additional arguments, unless they are already defined. If you do not pass any function names, all functions exported by the plug-in will be registered.
unshiftImportRealm(PLUG_IN[, FUNCTION...])#
Works like the method pushImportRealm()
, but already existing function definitions will be replaced.
setImportRealms(REALMS)#
Overwrites all existing function definitions, and imports all symbols from the optionally passed realms
setContext(CONTEXT)#
Sets the context the template processor is used in. The value you pass here will be used as the second argument in the call to isSafe()
for ViewImport
plug-ins (see isSafe(functionname, context)). Defaults to “HTML”.
setEscaper(CODEREF[, ARGS...])#
By default, interpolated output from unsafe functions is HTML escaped. You can override this behavior by passing a code reference to a filter function. The function should act as a classical filter, i. e. modify its argument and return the modified version.
You can optionally define arguments to the function call. These arguments are passed to the escaper function after the string to be escaped.
Subclassing#
You can also subclass your own parser classes from Imperia::View::Parser, if you want to extend or change its behavior.
errorOutput(MESSAGE)#
This method is called for errors encountered. Its single argument is the error message. If the default behavior does not suit you, you can define your own function here.