Re: [Phplib-users] template.inc : Why are blocks parsed over and over again?
Brought to you by:
nhruby,
richardarcher
From: Layne W. <la...@if...> - 2002-02-01 18:57:41
|
> Hi, > > because I am trying to understand how exactly template.inc > works, I put > some extra debug-stuff in subst(): > > if ($this->debug & 4) { > echo '<b>NOW SHOWING VARKEYS: </b>'.NL; > foreach ($this->varkeys as $k => $v) echo "KEY: $k --- VAL: $v ".NL; > echo '<b>NOW SHOWING VARVALS: </b>'.NL; > foreach ($this->varvals as $k => $v) echo "KEY: $k --- VAL: $v ".NL; > } > > > (NL is just "<BR>\n") > > This showed me, that *each* time I call parse, e.g to parse a block, > ALL variables are processed everytime. Is this a wanted behaviour? > > Could somebody explain this? It would be non-trivial (and take longer) to discover which variables exist in a given block. For many pages, this does not cause a performance hit, but we like to use templates to build selects and lists, so I use 2 instances of the template class. For a simple example: ***** sample.html ***** <table> <tr> <th>Name</th> <th>Email</th> </tr> <!-- BEGIN employee --> <tr> <td>{name}</td> <td><a href="mailto:{email}">{email}</a></td> </tr> <!-- END employee --> </table> ***** /sample.html ***** ***** sample.php ***** $t = new Template; $t2 = new Template; $t->set_file("content", "sample.html"); $t->set_block("content", "employee", "employees"); $t2->set_var("employee", $t->get_var("employee")); $db = new DB_MySQL; $db->query("select name, email from employee order by name"); while($db->next_record()) { $t2->set_var($db->Record); $t2->parse("employees", "employee", true); } $t->set_var("employees", $t2->get_var("employees")); $t->parse("out", "content"); $t->pparse("out"); ***** /sample.php ***** This has produced dramatic speed gains on my cluttered pages. Since I do this many times per page, I clear out all $t2 vars before starting another loop. I extend Template with the following function to make this easy: function clear_vars() { $this->varkeys = $this->varvals = array(); } Layne Weathers Ifworld Inc. |