NAME

HTML::Template::HashWrapper - Easy association with HTML::Template

SYNOPSIS

  use HTML::Template;
  use HTML::Template::HashWrapper;

  my $context = { var1 => 'Stuff',
                  var2 => [ { name => 'Name1', value => 'Val1', },
                            { name => 'Name2', value => 'Val2', },
                          ],
                };

  my $template = HTML::Template->new
    ( associate => HTML::Template::HashWrapper->new( $context ) );

  # Some::Object creates blessed hash references:
  my $something = Some::Object->new();
  my $wrapper = HTML::Template::HashWrapper->new( $something );
  my $template = HTML::Template->new( associate => $wrapper );

  # the wrapper keeps the original's interface:
  my $val1 = $something->somemethod( 251 );
  my $val2 = $wrapper->somemethod( 251 );

DESCRIPTION

HTML::Template::HashWrapper provides a simple way to use arbitrary hash references (and hashref-based objects) with HTML::Template's associate option.

new($ref) returns an object with a param() method which conforms to HTML::Template's expected interface:

  • param($key) returns the value of $ref->{$key}.

  • param() with no argument returns the set of keys.

  • param($key,$value) may also be used to set values in the underlying hash.

By default, HTML::Template::HashWrapper works by re-blessing the input object (or blessing, if the input is an unblessed hash reference) into a new package which extends the original package and provides an implementation of param(). If for some reason the input reference cannot be re-blessed, you may create the wrapper with this form of new():

    $wrapper = HTML::Template::HashWrapper->new( $obj, nobless => 1 );

The nobless option will force HashWrapper to create a new object, but leave the original reference in its original state of blessing.

In either case, all methods on the original object's class can be called on the newly created object.

NOTES

In theory, param() should also support setting multiple parameters by passing in a hash or hash reference. This interface currently does not support that, but HTML::Template only uses the two supported forms.

Should you decide to subclass HTML::Template::HashWrapper, be aware that in the nobless case, the package name for the base class is hardcoded.

AUTHOR

Greg Fast <gdf@speakeasy.net>

COPYRIGHT

Copyright 2003 Greg Fast (gdf@speakeasy.net)

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.