-
-
13 Jan 2004 14:35:13 UTC
- Distribution: HTML-Template-HashWrapper
- Module version: 1.3
- Source (raw)
- Browse (raw)
- Changes
- How to Contribute
- Issues
- Testers (430 / 0 / 12)
- Kwalitee
Bus factor: 0- 93.00% Coverage
- License: unknown
- Activity
24 month- Tools
- Download (4.86KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
- Dependencies
- unknown
- Reverse dependencies
- CPAN Testers List
- Dependency graph
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
associateoption.new($ref)returns an object with aparam()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.
new()will die if given something which is not a hash reference as an argument.The object returned by
$newretains its identity with its original class, so you can continue to use the object as normal (call its methods, etc).Internals
HTML::Template::HashWrapper works by re-blessing the input object (or blessing it, 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 (for example, you're using someone else's code which checks
ref($orig)when it should be usingisa()), you may use HTML::Template::HashWrapper::Plain:$wrapper = HTML::Template::HashWrapper::Plain->new( $obj );The
Plainwrapper object provides only the compliantparam()method, but not any of the original object's methods. The original object is left completely untouched. Most of the time this will be unneccesary.For purposes of testing the object type, HTML::Template::HashWrapper::Plain
isaHTML::Template::HashWrapper.HashWrapper works by creating an unique package whose
@ISAincludes both HashWrapper and the original package (if there is one) of the wrapped object.If you don't like the way the unique package names are generated, you can override
_GENERATE_PACKAGENAME(). Be aware that you will see strange behavior if this method does not return unique values (for a sufficient definition of "unique").Should you desire to subclass HashWrapper, you may wish to also subclass HashWrapper::Plain, which manages its state slightly differently.
OTHER
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.It should be possible to make this more efficient by memoizing the pairs of base package names, at the expense of some space for the mapping.
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.
Module Install Instructions
To install HTML::Template::HashWrapper, copy and paste the appropriate command in to your terminal.
cpanm HTML::Template::HashWrapperperl -MCPAN -e shell install HTML::Template::HashWrapperFor more information on module installation, please visit the detailed CPAN module installation guide.