[Templates] Modifying TT functions at run time

Simon Wilcox essuu@ourshack.com
04 Nov 2002 15:58:17 +0000


On Mon, 2002-11-04 at 15:16, Mark Fowler wrote:
> On 4 Nov 2002, Simon Wilcox wrote:
> 
> > Is it possible to add a function like this at runtime or should I just
> > bite the bullet and write a plugin ?
>
> There is no bullet to bite when writing plugins.  Stop pretending there 
> is ;-).  This is what I'd do (warning, untested code):

I wasn't :)

>   $Template::Stash::SCALAR_OPS->{imgsize} = \&image_size;

Doesn't this just add image_size as a vmethod, in which case it would be
called as [% imgfile.imgsize %] ?

Without testing it, I wonder if vmethods work on strings, i.e. 
[% "/path/to/image.jpg".imgsize %] ?

> This is adapted from the OccurancesV example from my Extending the
> Template Toolkit talk[1] (which I could swear you sat though at least 
> once.)  Darn, I really should find some time to write up things about 
> that.

I have indeed. Good talk too :)

I think my question wasn't worded correctly. I should have been more
specific about wanting to know if you can attach functions at runtime.
Which it would seem you can't. A plugin is the answer in that case.

Many thanks,

Simon.