Andrew Sterling Hanenkamp > Contentment-0.011_033 > Contentment::Generator::Template



Annotate this POD

Module Version: 0.08   Source  


Contentment::Generator::Template - Generator for Template Toolkit templates


  my $source = <<'END_OF_TEMPLATE';
  Hello [% args.who %]!

  [% META
      title = 'Testing'
      description = 'This is a test.' 
      kind = 'text/plain' %]

  my $generator = Contentment::Generator::Template->new({
      source => $source,

  my $title = $generator->get_property('title');

  $generator->generate({ who => 'World' });


A generator for Template Toolkit templates.

$generator = Contentment::Generator::Template->new(\%args)

This constructs a template generator. It takes the following arguments:

source (required)

This is the source template to generate from. It takes the same kinds of items as the "source" option to the constructor Contentment::Generator::Plain takes.

properties (optional, defaults to {})

This is the set of properties to start with. These will be overridden by any properties in the source.

variables (optional, defaults to {})

This allows the caller to set additional "global" variables to pass into the template. Arguments are normally passed in the "args" hash. This creates additional top-level variables. For example:

  my $generator = Contentment::Tempalte::Generator->new({
      source    => 'Hello [% who %]! How is [% args.who %]?',
      variables => { who => 'Homer' },

  $generator->generate({ who => 'Marge' });


  Hello Homer! How is Marge?

Don't use the "args" key. It will be clobbered during generation.

$source = $self->source

Returns the source. It also compiles the template the first time it's called.

$generator->properties (EXCEPTION)

This method throws an exception stating:

  Undefined subroutine &Contentment::Generator::Template::properties called

The reason for this exception is because there is no way to retrieve a list of properties from template toolkit templates.

$value = $generator->get_property($key)

Retrieves the property value for the given key, $key. This will fetch the property for the key from the proeprties set in source first, then fallback to properties set in the "properties" option to the constructor.

$generator->generate(\%args, \%vars)

Processes the template with the given arguments.

The first argument, %args, is passed as a hash named "args" to the template. The second argument, %vars, is passed directly into the template (with any key named "args" clobbered).



Handles the "Contentment::VFS::generator" hook. Specifies that the "Contentment::Generator::Template" generator should be used to generate files ending in ".tt2".


Currently, this generator compiles the source twice most of the time. This is due to the seemly obfuscated nature of the Template Toolkit compiler. I need to unravel the innards a bit more to learn how to use a Template::Document correctly.


Contentment::VFS, Contentment::Generator::Plain


Andrew Sterling Hanenkamp, <>


Copyright 2005 Andrew Sterling Hanenkamp. All Rights Reserved.

Contentment is distributed and licensed under the same terms as Perl itself.

syntax highlighting: