php - Lazy loading variables in templates -


currently doing this:

function load_template($script, $args){   extract($args);   require __dir__ . '/templates/' . $script; } 

in controller code:

// if home page requested load_template('home.php', array(   'title'   => get_title(),   'content' => get_content(),   ... )); 

the template php script like

<!doctype html> <html> <head>   <title> <?php echo $title; ?> </titlee> ... 

i wondering if it's possible lazy load these variables somehow, don't run get_title() or get_content() until template requests variable.

could possible, without creating template parser thingy? i'd stick simple .php scripts , html templates.


in short, i'm asking if it's possible auto-assign value variable when it's first requested.

$var = func();  // should not run  if($var){       // code above should run:)   echo $var;   // <- value assigned (don't run func() again) }  

in opinion, if not wish change template extract variables, can create example array know variables each template needs.

you may consider function (let's name caller) pass parameters , template name. caller choose variables required. idea factory class in oop.

i think there no other way, but...

while inserting template , use unexistent variable, warning shown. can make php throw exceptions in warnings , in try ... catch block parse it. think it's complicated , not worth effort.

edit

the third idea create objects instead of arrays. object keep $args variable. in template change <?php echo $title; ?> <?php echo $argument_object->gettitle(); ?>, , code gettitle() method. gettitle(), method not function, run on request.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -