A third parameter may be passed to the process method to specify a different output location. IO::Handle , Apache::Request , etc. After the optional third output argument can come an optional reference to a hash or a list of name, value pairs providing further options for the output.
The only option currently supported is binmode which, when set to any true value will ensure that files created but not any existing file handles passed will be set to binary mode. Alternately, the binmode argument can specify a particular IO layer such as :utf8.
The process method returns 1 on success or undef on error. The error message generated in the latter case can be retrieved by calling the error method.
Thus, the following are equivalent. Errors are represented in the Template Toolkit by objects of the Template::Exception class. If the process method returns a false value then the error method can be called to return an object of this class. The type and info methods can called on the object to retrieve the error type and information string, respectively.
This method is also overloaded onto the stringification operator allowing the object reference itself to be printed to return the formatted error string. The Template module delegates most of the effort of processing templates to an underlying Template::Service object. This method returns a reference to that object. The Template::Service module uses a core Template::Context object for runtime processing of templates.
This method is a simple wrapper around the Template::Context method of the same name. It returns a compiled template for the source provided as an argument.
The following list gives a short summary of each Template Toolkit configuration option. See Template::Manual::Config for full details.
Remove leading and trailing whitespace from template output default: 0. Allow absolute file names, e. Allow relative filenames, e. Name of error template or reference to hash array mapping error types to templates. Maximum number of compiled templates to cache in memory default: undef - cache all. Filename extension for compiled template files default: undef - don't compile.
Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. Any additional parameters supplied in parenthesis after the plugin name will be also be passed to the new constructor.
A reference to the current Template::Context object is passed as the first parameter. This is based on the assumption that the module is a regular Perl module rather than a Template Toolkit plugin so isn't expecting a context reference and wouldn't know what to do with it anyway. Named parameters may also be specified. These are collated into a hash which is passed by reference as the last parameter to the constructor, as per the general code calling interface.
The plugin may represent any data type; a simple variable, hash, list or code reference, but in the general case it will be an object reference. Methods can be called on the object or the relevant members of the specific data type in the usual way:. An alternative name may be provided for the plugin by which it can be referenced:. You can use this approach to create multiple plugin objects with different configurations.
This example shows how the format plugin is used to create sub-routines bound to variables for formatting text as per printf. This next example shows how the URL plugin can be used to build dynamic URLs from a base part and optional query parameters.
The CGI plugin is an example of one which delegates to another Perl module. All of the methods provided by the CGI module are available via the plugin. The MACRO directive allows you to define a directive or directive block which is then evaluated each time the macro is called.
Macros can be passed named parameters when called. These values remain local to the macro. Values passed to the macros are then mapped to these local variables. Other named parameters may follow these.
Here's another example, defining a macro for display numbers in comma-delimited groups of 3, using the chunk and join virtual method. The block will be evaluated each time the macro is called. Perl code is evaluated in the Template::Perl package. This can be used to access the functionality of the Template Toolkit to process other templates, load plugins, filters, etc. See Template::Context for further details. Through this, variable values can be retrieved and updated.
See Template::Stash for further details. Output is generated from the PERL block by calling print. The PERL block may contain other template directives. These are processed before the Perl code is evaluated. Exceptions may be thrown from within PERL blocks using die. They will be correctly caught by enclosing TRY blocks. The Template Toolkit parser reads a source template and generates the text of a Perl subroutine as output.
It then uses eval to evaluate it into a subroutine reference. This subroutine is then called to process the template, passing a reference to the current Template::Context object through which the functionality of the Template Toolkit can be accessed. The subroutine reference can be cached, allowing the template to be processed repeatedly without requiring any further parsing.
The PERL It is evaluated at "runtime" using eval each time the template subroutine is called. This is inherently flexible, but not as efficient as it could be, especially in a persistent server environment where a template may be processed many times. It is evaluated once at compile time and is stored in cached form as part of the compiled template subroutine.
The downside is that you must code much closer to the metal. For example, in a PERL block you can call print to generate some output. The critical section of the generated subroutine for this example would then look something like:. The Template Toolkit supports fully functional, nested exception handling. Any errors that occur within that block will be caught and can be handled by one of the CATCH blocks defined.
Errors are raised as exceptions objects of the Template::Exception class which contain two fields: type and info. The exception type is used to indicate the kind of error that occurred. The info field contains an error message indicating what actually went wrong. Within a catch block, the exception object is aliased to the error variable. You can access the type and info fields directly.
Each CATCH block may be specified with a particular exception type denoting the kind of error that it should catch. A CATCH block specified without any type, as in the previous example, is a default handler which will catch any otherwise uncaught exceptions. Remember that you can specify multiple directives within a single tag, each delimited by ' ; '. So the above example can be written more concisely as:.
The other specific exception caught here is of the file type. The output generated would be:. Note that the DEFAULT option disabled by default allows you to specify a default file to be used any time a template file can't be found. This will prevent file exceptions from ever being raised when a non-existent file is requested unless, of course, the DEFAULT file your specify doesn't exist. Errors encountered once the file has been found i.
Uncaught exceptions i. If the error isn't caught at any level then processing will stop and the Template process method will return a false value to the caller. The relevant Template::Exception object can be retrieved by calling the error method. We're using a variable to provide the name of the template we want to include, user. If an exception is uncaught then the FINAL block is processed before jumping to the enclosing block or returning to the caller. The output from the TRY block is left intact up to the point where an exception occurs.
For example, this template:. Exception types are hierarchical, with each level being separated by the familiar dot operator. A DBI. Similarly, an example. Note that the order in which CATCH handlers are defined is irrelevant; a more specific handler will always catch an exception in preference to a more generic or default one. In this example, a DBI. The first parameter is the exception type which doesn't need to be quoted but can be, it's the same as INCLUDE followed by the relevant error message which can be any regular value such as a quoted string, variable, etc.
It's also possible to specify additional positional or named parameters to the THROW directive if you want to pass more than just a simple message back as the error info field. In this case, the error info field will be a hash array containing the named arguments and an args item which contains a list of the positional arguments.
0コメント