| |
I think its
best to give an example of code where some variables aren't assigned a
value, but should be.
| book.tpl |
|
[...]
<body>
<table width="550" border="0">
<tr>
<td height="58" bgcolor="#CCCCCC">
<h2>Chapter {chapter_num}
{chapter_name}</h2>
</td>
</tr>
<tr>
<td height="29">
<h3>{subject}</h3>
</td>
</tr>
</table>
<p>
<!-- START BLOCK : text_row -->
Chapter Text {number}.<br>
<!-- END BLOCK : text_row -->
</p>
</body>
[...]
|
| mybook.php |
|
<?php
include( "./class.TemplatePower.inc.php");
$tpl = new TemplatePower( "./book.tpl" );
$tpl->prepare();
$tpl->assign( "chapter_num" , "7." );
for( $i=1; $i <= 10; $i++ )
{
$tpl->newBlock("text_row");
$tpl->assign("number", $i );
}
$tpl->assign( "chapter_name", "Variables" );
$tpl->assign( "subject" , "Basics" );
$tpl->printToScreen();
?>
|
Problem:
if we execute mybook.php we will see that the variables chapter_num
and number are assigned a value, but the variables
chapter_name and subject
are NOT.
Reason of failure:
The function newBlock is used to create a new "text_row" block.
By doing this the currentblock "pointer" is set to "text_row",
so TemplatePower assumes that the variables chapter_num
and number are children of the block "text_row".
Solution(s):
- Assign all variables
in a block, before creating children blocks or assigning variables.
$tpl->assign( "chapter_num" , "7." );
$tpl->assign( "chapter_name", "Variables" );
$tpl->assign( "subject" , "Basics" );
for( $i=1; $i <= 10; $i++ )
{
$tpl->newBlock("text_row");
$tpl->assign("number", $i );
}
- Use the variablename
in combination with the parent-blockname. In this case _ROOT,
because its the lowest level. You can write for example the variable
number as text_row.number
as well.
$tpl->assign( "chapter_num" , "7." );
for( $i=1; $i <= 10; $i++ )
{
$tpl->newBlock("text_row");
$tpl->assign("number", $i );
}
$tpl->assign( "_ROOT.chapter_name", "Variables" );
$tpl->assign( "_ROOT.subject" , "Basics" );
Ofcourse the variable chapter_num could
be written as _ROOT.chapter_num but in
this case isn't necessary, because we didn't create a new block and
are still in block_ROOT.
- Use the function
gotoBlock();
$tpl->assign( "chapter_num" , "7." );
for( $i=1; $i <= 10; $i++ )
{
$tpl->newBlock("text_row");
$tpl->assign("number", $i );
}
$tpl->gotoBlock("_ROOT");
$tpl->assign( "chapter_name", "Variables" );
$tpl->assign( "subject" , "Basics" );
|