For Programmers: Free Programming Magazines  


Home > Archive > PHP Smarty Templates > February 2005 > Calling a function to eval code before a template is displayed









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Calling a function to eval code before a template is displayed
Mark Watkins

2005-02-25, 4:02 pm

Hi there..

I'm trying to find a very simple way to eval a block of code that I retrieve
alongside with the templates from a database. I need to call this code before
displaying the template, and would rather do it during processing than having
to explicitly know it's going to be used, before the initial
$smarty->display("db:layout");

Being lazy, I don't want to have to do anything in my templates other than
what I usually do: {include file="db:whatever"} I just want smarty to
callback to one of my functions with at least the template_name, before it
does it's stuff.

Caching interferes with me trying to (mis)use the normal template fetch
related callbacks, timestamp & security callbacks kind of worked, but
sometimes get called twice.

Currently, I am preloading the bits of code I need run into derived classes
beforehand, and calling {$preloaded.modulename->execute()} inside my
templates, which runs my code, which can indirectly call smarty to display
it's own templates in the middle of the one it's displaying.

I could probably use autoloading to make these classes appear magically, but
I've already pinched that to generate a neat little generic class for
accessing database tables, and don't wanna have to use prefixes.

Any Ideas?


/Mark
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com