How to know if an object method was called from a template (was: Re: [Templates] Re: How to separate between hash and array in data returned from function)

Robin Smidsrød robin@smidsrod.no
Sun, 11 Feb 2007 19:37:57 +0100


Josh Rosenbaum wrote:
> Robin Smidsrød wrote:
>> One a sidenote, is there some way for an object method to know if it 
>> is being called from a template (instead of from normal perl code)? 
>> Is there some variable that is set that the function has access to 
>> which is not set otherwise? Or can I use some kind of function 
>> (caller) to inspect the call stack to figure it out? My object 
>> contains both read and write methods, and I want to make sure the 
>> write method calls are not callable from within a template, only from 
>> native perl code. Any pointers would be most helpful.
>
> If you have access to all the code, you could just set a global 
> variable right before processing the template and then check for that 
> in your function.
Hmmm... That is a reasonable suggestion. In my current setup this might 
work.

But my next question is then, how can I go about to set this variable 
automatically when $tt->process() is called? This way I don't have to 
change the many occurences of $tt->process() in my code. Are there some 
variable in the tt init code which could be set to a callback which is 
automatically run whenever I execute $tt->process?

I was looking through the code, and the only thing I could think of that 
could possible work is to use the [% CALL %] method, but then there is 
also the question of how to prepend this chunk of code in front of all 
the other templates without having to subclass Template...?

Suggestions?


-- Robin