Re: [Phplib-users] Problem repeating nested templates
Brought to you by:
nhruby,
richardarcher
From: Layne W. <la...@of...> - 2002-01-02 19:26:12
|
> >> How, using the current template.inc, can you repetitively use the > >> same template, expanded with different values, into other > >> templates? > >> > >> Example: > >> > >> <!-- BEGIN top --> (item) <!-- END top --> > >> > >> <!-- BEGIN item --> (links) <!-- END item --> > >> > >> <!-- BEGIN block --> (links) <!-- END block --> > >> > >> <!-- BEGIN links --> {link} <!-- END links --> > >> > >> And I want "top" containing multiple instances of "item" and > >> "block", with each of those sub-blocks containing a different > >> "links". The obvious approach doesn't work because the first > >> time I parse "links" to then build a block or item the template > >> is lost so I can't come back later with a new value for "link" > >> and gen a new value to later shove in a new block or item. > > > Although you are parseing the "links" block into several places, > > you need to name the target variables separately ({some_links}, > > {some_other_links}, etc). > > That's not the problem. > > The core of the problem is that set_block() replaces the instance of > the template in the file with {$name}. The core of the problem is > that is replaces $parent with {$name}, where $parent is the name of > the template (eg "links" above), and so can't be varied on a > per-call basis. You said that the block 'top' needs to contain multiple 'item's and multiple 'block's. I am leaving out the 'top' block because you didn't say anything about looping through multiple tops - if you need to do that it works the same way. ##### template.html ##### <!-- this was a block named top --> <!-- BEGIN item --> <!-- BEGIN links --> {link} <!-- END links --> <!-- END item --> <!-- BEGIN block --> (block_links) <!-- END block --> <!-- this was a block named top --> ###### page.php ###### $t = new Template; $db = $db2 = new DB_Whatever; $t->set_file("file", "template.html"); $t->set_block("file", "item", "items"); $t->set_block("item", "links", "item_links"); $t->set_block("file", "block", "blocks"); // dummy queries - you've got your data retrieval figured out // get all of the 'item's $db->query("select item from some_table"); while($db->next_record()) { // get all of the 'item' links $db2->query(sprintf("select link from item_links_table where item = '%s'", $db->f("item"))); while($db2->next_record()) { $t->set_var($db2->Record); $t->parse("item_links", "links", true); } $t->parse("items", "item", true); $t->set_var("item_links", ""); } // now get all of the 'block's $db->query("select block from some_table"); while($db->next_record()) { // get all of the 'block' links $db2->query(sprintf("select link from block_links_table where block = '%s'", $db->f("block"))); while($db2->next_record()) { $t->set_var($db2->Record); $t->parse("block_links", "links", true); } $t->parse("blocks", "block", true); $t->set_var("block_links", ""); } $t->pparse("file"); |