Re: accessing template variables in perl code

Dave Howorth <dhoworth-fDajt2Yx3S8pY9vWkoisglpr/1R2p/[email protected]> Mon, 11 Nov 2013 15:12:43 +0000
Newsgroups gmane.comp.lang.perl.modules.template-toolkit
Organization MRC Laboratory for Molecular Biology
Message-ID <[email protected]>
Dave Howorth wrote:
> Felipe Gasper (cPanel) wrote:
>> On 11/8/13 10:54 AM, Dave Howorth wrote:
>>> I expect my brain is fried and I'm doing it all wrong, but I'd
>>> appreciate advice about how to access template variables in perl code.
>>>
>>> I have a perl function that retrieves some data and returns it. I've put
>>> the function in the stash so I can call it in a template. It needs to
>>> decide what data to retrieve based on the name of the template, among
>>> other things. Is there a way it can access the template.name directly,
>>> to avoid having to pass the value as an argument every time?
>>>
>>> Or a better way to approach the problem?
>> Sounds like you should make a plugin instead, then call the plugin; that
>> plugin will be able to access the context(), stash(), and other
>> interesting stuff.
> 
> Thanks. That looks like it will do the job if I rearrange my code a bit.

Just in case anybody's interested, what I did in the end was define the
function in the main application, passing in the Template object ($tt)
as a closure. The function can then access the stash and page name
through the $tt object. Then I put the function in the stash so the
template can use it. Saves making a separate plugin module.

Cheers, Dave