Support ⇒ Explain Please :: Archives ⇒ Reusing Templates several times on one page. :: Archived ⇒ Community Forums ⇒ CPG Dragonfly™ CMS
Forum IndexExplain Please

Archived ⇒ Reusing Templates several times on one page.


I think I might have missed something about templates which is causing me quite a bit of distress. So here I go trying to explain the problem. I have been able to figure out how to have a template repeat it self over and over again do say display a list of information. This works fine but what doesn't seem to work is if I repeat a template say 3 times then insert a diffrent templated lets say a new header then repeat the original template another 3 times, what I get as an output is the templated repeated 6 times then the header. Is there no way to keep the templates in order when they get printed?

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Windows_NT/Apache/1.3.33 (Win32) PHP/4.3.10/4.1.9-max/4.3.10/9.0.4.0


Templates will get parsed (printed) in the order in which they are placed within your script. It sounds to me like you're using assign_vars when you need to be using assign_block_vars.... but it's hard to say without seeing the code.


Both assign_vars and assign_block_vars take on, basically, the same syntax, but do very different things. The sytax, or structure, of assign_vars is like so;

$cpgtpl->assign_vars(array( 'L_LABEL' => _SOMELABEL, 'I_IMAGE' => $someimage, 'U_URL' => $someurl, 'S_STRING' => $somestring ));

and assign_block_vars;

$cpgtpl->assign_block_vars('block_label', array( 'L_LABEL' => _SOMELABEL, 'I_IMAGE' => $someimage, 'U_URL' => $someurl, 'S_PROJECTNAME' => $somestring ));

The major difference between the two functions is that assign_vars is used to hold simple variables, where each defined key of the array holds one value. assign_block_vars does basically the same thing, except each defined key will hold multiple values.

Now, for using these variables inside an html template. Let's use the forums to think about the use of each. Take the main forum index, for example. The page is created with a table of information, with labels at the top of each column. These labels are used once (although they can be used as many times in the template as you wish) and will be assigned with assign_vars. To use these labels you would simply use the name of the defined variable, wrapped in curly brackets, like so {L_SOMELABEL}. An example of creating a table with column labels, is like so;

<table class="forumline" align="center" width="100%" border="0" cellspacing="1" cellpadding="3"> <tr> <th width="100%" colspan="2">{L_SOMELABEL}</th> <th align="center">{L_SOMEOTHERLABEL}</nobr></th> <th align="center">{L_YETANOTHERLABEL}</nobr></th> </tr>

Now, creating multiple rows of data underneath these labels is where we're going to use assign_block_vars. If you think about a while routine as a 'block' of code that is executed, it will help understanding assign_block_vars.

while (list($var1, var2, var3) = $db->sql_fetchrow($result)) { this code gets run as long as the while routine returns true (as long as there is data coming from the database) }

As you can see from the code above, the more the while routine runs, the more data is created and needs to be defined so we can use them inside our template. Using assign_vars would not work here as the first time the routine is run $var1, $var2 and $var3s data would be defined, and each successive time the routine is run, the previous data would be over-written with the newly assigned data. Once the routine is finished, we would only have one set of variables, which would be the last set to come from the database... not what we want.

To solve this, we use assign_block_vars. assign_block_vars stores the data in rows of an array, with each row being unique. Let's take a look at the while block, with assign_block_vars in place.

while (list($var1, var2, var3) = $db->sql_fetchrow($result)) { [i]this code gets run as long as the while routine returns true (as long as there is data coming from the database)[/i] $cpgtpl->assign_block_vars('block_label', array( 'S_VAR1' => $var1, 'S_VAR2' => $var2, 'S_VAR3' => $var3 )); }

Using our example of the forums, we are now trying to display multiple rows underneath our column labels, which represent multiple threads in the forum. Let's say we're trying to display topic, author and last post date (to use our 3 vars above). We cannot simply use the variable, wrapped in curly brackets, as we did above. Why? Because we are holding multiple rows of data and the system needs to know which row to display, and to keep displaying rows until there are no more. We do this, inside the html template, by using BEGIN and END.

Accessing the variables is also different than we did with assign_vars, as we need to know which set of variables we want to access. This is done by adding the name/label of the block to the variable you want to display, like so --> {block_label.S_VAR1}

<!-- BEGIN project_rows --> <tr> <td class="row1" align="center" colspan="2">{project_rows.S_VAR1}</td> <td class="row1">{project_rows.S_VAR2}</td> <td class="row1" align="center">{block_label.S_VAR3}</td> </tr> <!-- END project_rows -->

There are two important parts to using assign_block_vars.

1) you assign a name, or label, to the block variables (block_label was used above as an example) so that you have something to reference the block variables with.

2) you use BEGIN and END to loop through and display the block variables.


Probably the easiest way for someone to help you is for you to post your code.

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
RHE Linux / 2.0.52 / 4.0.22 / 4.3.10 / 9.0.6.1-CVS


Looks like another Wiki addition safecracker4hire 😉

DonationsPro for DragonflyCMS & SMF

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):


That is actually a reprint from the Debuggers General forum (HERE)... it was a topic that I started to attract others to put in their notes/comments about the templating system to get it documented.... it died after 8 posts.... Sad

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
RHE Linux / 2.0.52 / 4.0.22 / 4.3.10 / 9.0.6.1-CVS


safecracker4hire is probably right, if you are trying to use a template in the same page multiple times, all in one big chunk of code, then using the assign_block_vars() method would probably work best. If you're trying to use the same template file in different locations on the same page, then call display() with false as the second parameter:
$cpgtpl->display( 'body', false );

For example, this is useful for creating two calendar on one page in different locations. Just make sure you call display() before you start overwriting the values in the template. Here's an example:
<?php if (!defined('CPG_NUKE')) { exit; } require_once('header.php'); OpenTable(); $cpgtpl->set_filenames( array( 'index' => 'Sample/index.html' ) ); $cpgtpl->assign_vars( array( 'S_STRING' => 'First time through', ) ); $cpgtpl->display( 'index' ); $cpgtpl->assign_vars( array( 'S_STRING' => 'Second time through', ) ); $cpgtpl->display( 'index', false ); CloseTable();

and in your template file:
{S_STRING} <br />

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Win, Linux/1.3.34/4.0.26, 5.0.19/4.3.10, 5.1.2, 8.1.4(PostgresSQL)/9.0.6.1, CVS


Thanks Biggles thats the answer I was looking for

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Windows_NT/Apache/1.3.33 (Win32) PHP/4.3.10/4.1.9-max/4.3.10/9.0.4.0


safecracker4hire wrote
it was a topic that I started to attract others to put in their notes/comments about the templating system to get it documented.... it died after 8 posts.... Sad


dragonflycms.org/assign_block_vars
dragonflycms.org/assign_vars

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Fedora 25 / Apache 2.4.27 / MariaDB 10.1.26 / PHP 7.1.10 / Mercurial


I realise that the dev docs are being populated over time, but the text that is there just doesn't cut it.

assign_block_vars

boolean assign_block_vars()

Assigns the given variables only to the specified block and keep them out of the global process.

$cpgtpl->assign_block_vars('comment', array( 'IS_NESTED' => ($comments == 0 && $mode != 'flat'), 'IS_NESTED_END'=> false, 'IS_FIRST' => false, 'IS_FIRST_END' => false, 'IS_LIST' => false ));


Means what to most people?

Take a poll.... how many of you reading this now know what assign_block_vars is and does via the quote above? Click the link for assign_vars... do you now understand assign_vars? How about what I_, U_, S_, G_ or any other prefix means?

Me? I simply went through the code and followed it logically, understanding what was happening at each step and learned how to develop with the current templating system... but not everyone can do that.

This is why I tried to start a topic to get comments flowing and a real Wiki page done. I think this is a very weak point and why module/block development is limited to a few that actually understand the system.

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
RHE Linux / 2.0.52 / 4.0.22 / 4.3.10 / 9.0.6.1-CVS


Publish in Wiki so that more than a few staff can see it - it can evolve from there.

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):


Ok I have Finally Found an Example So hopefully somebody can help me out here


PHP COde used to Call the Template
//******************************************************************************************************************* function BuildVoterList($Tabid) { Global $db, $prefix, $module_name, $MAIN_CFG, $cpgtpl; $config = $MAIN_CFG[''.$module_name.'']; $PrefixData = $config['Database']; list($Rankid) = $db->sql_ufetchrow('SELECT content_id FROM '.$prefix.'_'.$PrefixData.'_tabcontent WHERE tab_id = '.$Tabid.'', SQL_NUM, __FILE__); $ListQuestions = $db->sql_query('SELECT * FROM '.$prefix.'_simpleratings_questions WHERE rating_id ='.$Rankid.'', false, __FILE__,__LINE__); $cpgtpl->assign_block_vars('votelistheader', array()); while($Question = $db->sql_fetchrow($ListQuestions)) { $cpgtpl->assign_block_vars('votelistcolumnheader', array()); $ListVotes = $db->sql_query('SELECT * FROM '.$prefix.'_simpleratings_votes WHERE ques_id ='.$Question['item_id'].' AND tab_id='.$Tabid.'', false, __FILE__,__LINE__); while($Vote = $db->sql_fetchrow($ListVotes)) { $cpgtpl->assign_block_vars('votelistcolumnitem', array()); } $cpgtpl->assign_block_vars('votelistcolumnfooter', array()); } $cpgtpl->assign_block_vars('votelistfooter', array()); }

The HTML TEMPLATE FILE (Just the part of interest is included)
<!-- BEGIN votelistheader --> <div id="BlockHeaderOffset"><p id="title">Question</p> </div> <div id="ColumnContainer"> <!-- END votelistheader --> <!-- BEGIN votelistcolumnheader --> <Div id="Column"> <ul id="List"> <!-- END votelistcolumnheader --> <!-- BEGIN votelistcolumnitem --> <li id="ListItem">UserName</li> <!-- END votelistcolumnitem --> <!-- BEGIN votelistcolumnfooter --> <ul> </div> <!-- END votelistcolumnfooter --> <!-- BEGIN votelistfooter --> <div id="c"></div> </div> </div> <!-- END votelistfooter -->

Expected Output

<div id="BlockHeaderOffset"><p id="title">Question</p> </div> <div id="ColumnContainer"> <Div id="Column"> <ul id="List"> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> </ul> </div> <Div id="Column"> <ul id="List"> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> </ul> </div> <Div id="Column"> <ul id="List"> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> </ul> </div> <Div id="Column"> <ul id="List"> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> </ul> </div> <div id="c"></div> </div> </div>

Actual Output


<div id="BlockHeaderOffset"><p id="title">Question</p> </div> <div id="ColumnContainer"> <Div id="Column"> <ul id="List"> <Div id="Column"> <ul id="List"> <Div id="Column"> <ul id="List"> <Div id="Column"> <ul id="List"> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <li id="ListItem">UserName</li> <ul> </div> <ul> </div> <ul> </div> <ul> </div> <div id="c"></div> </div> </div>

It can be Very Easily be seen that the the template is not being displayed in order of contruction in the php file but instead it is outputting all of the first Template then all of the second template and then all of the third template

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Windows_NT/Apache/1.3.33 (Win32) PHP/4.3.10/4.1.9-max/4.3.10/9.0.4.0


PartyMarty, it seem to be confused how assign_block_vars() works. When you add values using assign_block_vars, you are essentially adding that information to an array. For example, the following assign_block_vars() code:
$cpgtpl->assign_block_vars('parentblockid', array( 'S_PARENT_KEY' => '1' ) ); $cpgtpl->assign_block_vars('parentblockid', array( 'S_PARENT_KEY' => '2' ) ); $cpgtpl->assign_block_vars('parentblockid', array( 'S_PARENT_KEY' => '3' ) );

can be thought of like this:
[parentblockid] => Array ( [0] => Array ( [S_PARENT_KEY] => 1 ) [1] => Array ( [S_PARENT_KEY] => 2 ) [2] => Array ( [S_PARENT_KEY] => 3 ) )

So, when you have this in your template:
<!-- BEGIN parentblockid --> {S_PARENT_KEY}<br /> <!-- END parentblockid -->

it will produce this:
1 2 3

You are going through the "parentblockid" section in the template and printing every value.


What you're trying to do is nest the information, which is a little more complicated. To add a child section to a parent section, you specify the parent block name then a period then the child block name, for example "blockidparent.blockidchild". So from our above example:
$cpgtpl->assign_block_vars('parentblockid', array( 'S_PARENT_KEY' => '1' ) ); $cpgtpl->assign_block_vars('parentblockid.childblockid', array( 'S_CHILD_KEY' => '1' ) ); $cpgtpl->assign_block_vars('parentblockid.childblockid', array( 'S_CHILD_KEY' => '2' ) ); $cpgtpl->assign_block_vars('parentblockid.childblockid', array( 'S_CHILD_KEY' => '3' ) ); $cpgtpl->assign_block_vars('parentblockid', array( 'S_PARENT_KEY' => '2' ) ); $cpgtpl->assign_block_vars('parentblockid.childblockid', array( 'S_CHILD_KEY' => '4' ) ); $cpgtpl->assign_block_vars('parentblockid.childblockid', array( 'S_CHILD_KEY' => '5' ) ); $cpgtpl->assign_block_vars('parentblockid.childblockid', array( 'S_CHILD_KEY' => '6' ) ); $cpgtpl->assign_block_vars('parentblockid', array( 'S_PARENT_KEY' => '3' ) ); $cpgtpl->assign_block_vars('parentblockid.childblockid', array( 'S_CHILD_KEY' => '7' ) ); $cpgtpl->assign_block_vars('parentblockid.childblockid', array( 'S_CHILD_KEY' => '8' ) ); $cpgtpl->assign_block_vars('parentblockid.childblockid', array( 'S_CHILD_KEY' => '9' ) );

which can be thought of like this:
[parentblockid] => Array ( [0] => Array ( [S_PARENT_KEY] => 1 [childblockid] => Array ( [0] => Array ( [S_CHILD_KEY] => 1 ) [1] => Array ( [S_CHILD_KEY] => 2 ) [2] => Array ( [S_CHILD_KEY] => 3 ) ) ) [1] => Array ( [S_PARENT_KEY] => 2 [childblockid] => Array ( [0] => Array ( [S_CHILD_KEY] => 4 ) [1] => Array ( [S_CHILD_KEY] => 5 ) [2] => Array ( [S_CHILD_KEY] => 6 ) ) ) [2] => Array ( [S_PARENT_KEY] => 3 [childblockid] => Array ( [0] => Array ( [S_CHILD_KEY] => 7 ) [1] => Array ( [S_CHILD_KEY] => 8 ) [2] => Array ( [S_CHILD_KEY] => 9 ) ) ) )

So, when you have this in your template:
<!-- BEGIN parentblockid --> {parentblockid.S_PARENT_KEY}<br /> <!-- BEGIN childblockid --> &nbsp;&nbsp;{parentblockid.childblockid.S_CHILD_KEY}<br /> <!-- END childblockid --> <!-- END parentblockid -->

it will produce this:
1 1 2 3 2 4 5 6 3 7 8 9


So..........
In summary, your php code should probably look like this:
function BuildVoterList($Tabid) { global $db, $prefix, $module_name, $MAIN_CFG, $cpgtpl; $config = $MAIN_CFG[''.$module_name.'']; $PrefixData = $config['Database']; list($Rankid) = $db->sql_ufetchrow('SELECT content_id FROM '.$prefix.'_'.$PrefixData.'_tabcontent WHERE tab_id = '.$Tabid.'', SQL_NUM, __FILE__); $ListQuestions = $db->sql_query('SELECT * FROM '.$prefix.'_simpleratings_questions WHERE rating_id ='.$Rankid.'', false, __FILE__,__LINE__); $cpgtpl->assign_block_vars('votelist', array()); while($Question = $db->sql_fetchrow($ListQuestions)) { $cpgtpl->assign_block_vars('votelist.column', array()); $ListVotes = $db->sql_query('SELECT * FROM '.$prefix.'_simpleratings_votes WHERE ques_id ='.$Question['item_id'].' AND tab_id='.$Tabid.'', false, __FILE__,__LINE__); while($Vote = $db->sql_fetchrow($ListVotes)) { $cpgtpl->assign_block_vars('votelist.column.item', array()); } } }

and in your template:
<!-- BEGIN votelist --> <div id="BlockHeaderOffset"> <p id="title">Question</p> </div> <div id="ColumnContainer"> <!-- BEGIN column --> <div id="Column"> <ul id="List"> <!-- BEGIN item --> <li id="ListItem">UserName</li> <!-- END item --> </ul> </div> <!-- END column --> <div id="c"></div> </div> <!-- END votelist -->

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Win, Linux/1.3.34/4.0.26, 5.0.19/4.3.10, 5.1.2, 8.1.4(PostgresSQL)/9.0.6.1, CVS


Sweet! Wink

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
RHE Linux / 2.0.52 / 4.0.22 / 4.3.10 / 9.0.6.1-CVS


WOW nice.

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Linux/1.3.33/4.4/4.3.11


Works like a Charm! Thanks Biggles! This info should really be made into a wiki page so that it doesn't get lost. I'm sure I am not the only one who didn't know about this.

Again Biggle thanks for the information!

Server specs (Server OS / Apache / MySQL / PHP / DragonflyCMS):
Windows_NT/Apache/1.3.33 (Win32) PHP/4.3.10/4.1.9-max/4.3.10/9.0.4.0

All times are UTC