Template.assign block vars
From phpBB Development Wiki
template::assign_block_vars –– Assign key variable pairs from an array to a specified block.
Contents |
Description
bool template:: assign_block_vars (String $blockname, array $vararray )
The Template.assign_var and Template.assign_vars method doesn't allow you to create loops inside a template, in order to achieve this you'll have to user the assign_block_vars method of the template object.
- Note: The means of transporting data to the template is the use of template variables.. By convention, template variable names are always all-uppercase where block names are always all-lowercase. Variables output to the user are enclosed in curly braces, and are formatted in the following way: {blockname.L_VARIABLE_NAME}.
Parameters
| Parameter | Usage |
|---|---|
| blockname | The name of the block in which the variables will be assigned. |
| vararray | Array containing keys and values. Keys are used within the template and the associated value will be displayed to the user. |
Return Values
Returns true
Examples
Example #1 Single block
Example of a block in a HTML template:
<div>
<!-- BEGIN somerow -->
<ul>
<li>{somerow.VAR1}</li>
<li>{somerow.VAR2}</li>
</ul>
<!-- END somerow -->
</div>
Example php controller to assign this block:
// Loop twice
for ($i = 0; $i < 2; $i++)
{
$template->assign_block_vars('somerow', array(
'VAR1' => "Just a string with the value of \$i: $i",
'VAR2' => ($i * 4),
));
}
Example #2 Nested blocks
Example of PHP Controller assigning the values to the nested blocks:
// setup an example multi-dimensional array...
$item_ary = array(
'food' => array(
'banana',
'apple',
'pie',
'ice-cream',
),
'animal' => array(
'Horse',
'Dog',
'Cat',
),
);
foreach ($item_ary as $category => $item_ary)
{
// categories in this example are "food" and "animal"
$template->assign_block_vars('block1', array(
'CATEGORY' => $category,
));
// each item within the category is assigned to the second block.
foreach ($item_ary as $key => $item)
{
$template->assign_block_vars('block1.block2', array(
'ITEM' => $item,
'ITEM_NUM' => $key + 1,
));
}
}
Example of nested blocks in an HTML template:
<div>
<!-- BEGIN block1 -->
<ul>
<li>{block1.CATEGORY}</li>
<li>
<ul>
<!-- BEGIN block2 -->
<li>{block1.block2.ITEM_NUM}: {block.block2.ITEM}</li>
<!-- END block2 -->
</ul>
</li>
</ul>
<!-- END block1 -->
</div>
See Also

