NAME
Text::QuickTemplate - A simple, lightweight text fill-in class.
VERSION
This documentation describes v0.02 of Text::QuickTemplate, July 24, 2005.
SYNOPSIS
$template = Text::QuickTemplate->new($string, \%options);
$result = $template->fill(\%values);
OPTIONS
delimiters => [ '{{', '}}' ]; # may be strings
delimiters => [ qr/\{\{/, qr/\{\{/ ]; # and/or regexps
DESCRIPTION
There are many templating modules on CPAN. They're all far, far more powerful than Text::QuickTemplate. When you need that power, they're wonderful. But when you don't, they're overkill.
This module provides a very simple, lightweight, quick and easy templating mechanism for when you don't need those other, powerful but cumbersome, modules.
You create a template object that contains the boilerplate text. Substitution placeholders within the text are indicated by keywords, set off from the surrounding text by a pair of delimiters. (By default the delimters are {{
and }}
, since double curly braces are rare in programming languages [and natural languages]).
Keywords between the delimiters must be comprised entirely of "word" characters (that is, alphabetics, numerics, and the underscore), and there must be no spaces or other characters between the keyword and its delimiters. This strictness is considered a feature.
When it is necessary to render the final text (with placeholders filled in), you use the fill
method, passing it one or more references of hashes of values to be substituted into the original boilerplate text. The special value $DONTSET indicates that the keyword (and its delimiters) are to remain in the boilerplate text, unsubstituted.
That's it. No control flow, no executable content, no filesystem access. Never had it, never will.
METHODS
- new
-
$template_object = Text::QuickTemplate->new($boilerplate, \%options);
Creates a new Text::QuickTemplate object. The boilerplate text string parameter is mandatory; the hashref of options is optional.
Currently, the only option permitted is
delimiters
, which is a reference to an array of two strings (or compiled regular expresions): a starting delimiter and an ending delimiter. - fill
-
$result_string = $template->fill($hashref); $result_string = $template->fill($hashref, $hashref, ...);
Replaces all of the placeholders within the template with values from the hashref(s) supplied.
For each placeholder, the hashrefs are examined in turn for a matching key. As soon as one is found, the template moves on to the next placeholder. Another way of looking at this behavior is "The first hashref that fulfills a given placeholder -- wins."
If the resulting value is
$DONTSET
, the placeholder is left intact in the template.If no value for a placeholder is found among any of the hash references passed, an exception is thrown.
- pre_fill
-
$template->pre_fill($hashref, ...);
Specifies one or more sets of key=>value pairs to be used by the
fill
method in addition to (and higher priority than) the ones passed tofill
.This can be useful if some template values are set when the template is created, but the template is filled elsewhere in the program, and you don't want to pass variables around.
- clear_values
-
$template->clear_values();
Removes any pre_filled hash references in the object.
EXAMPLES
$book_t = Text::QuickTemplate->new('<i>{{title}}</i>, by {{author}}');
$bibl_1 = $book_t->fill({author => "Stephen Hawking",
title => "A Brief History of Time"});
# yields: "<i>A Brief History of Time</i>, by Stephen Hawking"
$bibl_2 = $book_t->fill({author => "Dr. Seuss",
title => "Green Eggs and Ham"});
# yields: "<i>Green Eggs and Ham</i>, by Dr. Seuss"
$bibl_3 = $book_t->fill({author => 'Isaac Asimov'});
# Dies with "could not resolve the following symbol: title"
$bibl_3 = $book_t->fill({author => 'Isaac Asimov',
title => $DONTSET });
# yields: "<i>{{title}}</i>, by Isaac Asimov"
EXPORTS
This module exports the symbol $DONTSET
into the caller's namespace.
REQUIREMENTS
This module is dependent upon the following other CPAN modules:
Readonly
Exception::Class
DIAGNOSTICS
Text::QuickTemplate uses Exception::Class objects for throwing exceptions. If you're not familiar with Exception::Class, don't worry; these exception objects work just like $@
does with die
and croak
, but they are easier to work with if you are trapping errors.
All exceptions thrown by Text::QuickTemplate have a base class of QuickTemplate::X. You can trap errors with an eval block:
eval { $letter = $template->fill(@hashrefs); };
and then check for errors as follows:
if (QuickTemplate::X->caught()) {...
You can look for more specific errors by looking at a more specific class:
if (QuickTemplate::X::KeyNotFound->caught()) {...
Some exceptions provide further information, which may be useful for your exception handling:
if (my $ex = QuickTemplate::X::OptionError->caught())
{
warn "Bad option: " . $ex->name();
...
If you choose to (or cannot) handle a particular type of exception (for example, there's not much to be done about a parameter error), you should rethrow the error:
if (my $ex = QuickTemplate::X->caught())
{
if ($ex->isa('QuickTemplate::X::SomethingUseful'))
{
...
}
else
{
$ex->rethrow();
}
}
Parameter errors
Class:
QuickTemplate::X::ParameterError
You called a Text::QuickTemplate method with one or more bad parameters. Since this is almost certainly a coding error, there is probably not much use in handling this sort of exception.
As a string, this exception provides a human-readable message about what the problem was.
Option errors
Class
QuickTemplate::X::OptionError
There's an error in one or more options passed to the constructor
new
.This exception has one method,
name()
, which returns the name of the option that had a problem (for example, 'delimiter
').As a string, this exception provides a human-readable message about what the problem was.
Unresolved symbols
Class
QuickTemplate::X::KeyNotFound
One or more subsitution keywords in the template string were not found in any of the value hashes passed to
fill
orpre_fill
. This exception is thrown byfill
.This exception has one method,
symbols()
, which returns a reference to an array containing the names of the keywords that were not found.As a string, this exception resolves to
"Could not resolve the following symbols:"
followed by a list of the unresolved symbols.Internal errors
Class
QuickTemplate::X::InternalError
Something happened that I thought couldn't possibly happen. I would appreciate it if you could send me an email message detailing the circumstances of the error.
AUTHOR / COPYRIGHT
Eric J. Roode, roode@cpan.org
To avoid my spam filter, please include "Perl", "module", or this module's name in the message's subject line, and/or GPG-sign your message.
Copyright (c) 2005 by Eric J. Roode. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.