[Templates] auto filtering

David Dick david_dick@mbox.com.au
Mon, 18 Nov 2002 13:31:51 +1100


This is a multi-part message in MIME format.

--Boundary_(ID_br4/VQUuOu9F74s0u+cv8w)
Content-type: text/plain; charset=us-ascii
Content-disposition: inline
Content-transfer-encoding: 7BIT

Solved the problem by using a custom stash (example attached) and
overriding the get method.  As a side note, also needed to override the
private _assign method due to a line in Template::Stash that read

    if ($rootref eq 'HASH' || $rootref eq __PACKAGE__) {

which i needed to translate to

    if ($rootref eq 'HASH' || UNIVERSAL::isa($root, __PACKAGE__)) {

to inherit the Template::Stash object.
Cheers
-Dave

----- Original Message -----
From: darren chamberlain <dlc@users.sourceforge.net>
Date: Wednesday, November 13, 2002 5:25 am
Subject: Re: [Templates] auto filtering

> * Mark Fowler <mark@twoshortplanks.com> [2002-11-12 11:20]:
> > On Tue, 12 Nov 2002, Andy Wardley wrote:
> > > You could write a custom stash which puts a wrapper around the 
> > > get() method, filtering all the output that it returns.
> > 
> > Putting it at the stash level seems to be a mistake to me...you 
> often> want to manipulate the data and do a lot of stuff and not 
> have to
> > worry about filtering it until the last matter.
> 
> I came to exactly the same conclusion when I decided to spend a few
> minutes trying to implement it.
> 
> > Since we're talking about being lazy here, I'd probably 
> implement it
> > as an option to auto filter interpolated variables.  So in this
> > example:
> > 
> >   This is a [% foo %] with very many $bar.
> > 
> > foo would not be filtered, but $bar would be.  This seems to 
> give you
> > the right degree of balance between convenience and flexibility.
> 
> Interesting... as long as we can assume that we're talking about HTML.
> I would hate for my GD images to be HTML encoded, for example.  That's
> why filters are optional, and need to be specified explicitly.
> 
> (darren)
> 
> -- 
> How can I believe in God when just last week I got my tongue 
> caught in
> the roller of an electric typewriter?
>    -- Woody Allen
> 
> _______________________________________________
> templates mailing list
> templates@template-toolkit.org
> http://lists.ourshack.com/mailman/listinfo/templates
> 

--Boundary_(ID_br4/VQUuOu9F74s0u+cv8w)
Content-type: application/octet-stream; NAME=MyStash.pm
Content-disposition: attachment; filename=MyStash.pm
Content-trans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