|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
malou Smarty n00b
Joined: 15 Jul 2003 Posts: 3
|
Posted: Wed Jul 16, 2003 6:38 pm Post subject: Problem with a template included twice |
|
|
I have a problem I can't deal with. Here is some code.
Code: |
-----------
page.tpl
-----------
{section name=k loop=$pages.type}
{if $pages.type[k] == 'text'}
{$pages.content[k]}
<br clear=all>
{/if}
{if $pages.type[k] == 'offer'}
{include file="offer.tpl" offer=$offer[k]}
{/if}
{/section}
------------
offer.tpl
------------
{section name=i loop=$offer.groups}
<span class=smallHeader>{$offer.groups[i].name}</span><br>
{/section}
|
[php:1:42ce3bc2d1]<?php
$smarty -> assign('offer[1]',$offer1);
$smarty -> assign('offer[3]',$offer2);
?>[/php:1:42ce3bc2d1]
The section "k" in page.tpl is looping three times:
1. $pages.type[k] equals offer and the file offer.tpl is included
2. $pages.type[k] equals text and I put some text here //it works fine
3. $pages.type[k] equals equals offer again and the file offer.tpl is included
I have two arrays "offer1" and "offer2". First I want to assign in first loop and second in third loop. When I try to make it as above it doesn't work.
I use Smarty 2.5.0
Thanks |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Jul 16, 2003 7:06 pm Post subject: |
|
|
I suggest using {foreach} instead of {section}. IMHO, it is almost always the more appropriate of the native looping structures Smarty provides and should lead to less headaches. In this case, it should result in the proper number of loop iterations. |
|
Back to top |
|
malou Smarty n00b
Joined: 15 Jul 2003 Posts: 3
|
Posted: Wed Jul 16, 2003 10:32 pm Post subject: |
|
|
Thanks for help. I've made as you suggested but I've also checked and I think it's not a problem with iteration. I've made some changes and code looked like this...
Code: |
---------
page.tpl
---------
{foreach name=outer key=key item=item from=$pages.type}
{if $item == 'offer'}
{include file="offer.tpl" offer=$offerxx[$key]}
{/if}
{/foreach}
|
[php:1:f59057e0eb]<?php
$smarty -> assign('offerxx[0]',$offer1);
$smarty -> assign('offerxx[1]',$offer2);
?>[/php:1:f59057e0eb]
It doesn't work... however the variable $offerxx[0] nad $offerxx[1] are visible in the smarty debug console they are not reachable in foreach loop.
Even when I put a line {$offerxx[0]} into a foreach I still can't access the array. I've made some tests and when I used a tag similar to this {$offerxx[0]} the script was not displaying the variable (however smarty dedug console was showing the variable).
Then...
I've made some changes and script looked like this...
Code: |
---------
page.tpl
---------
{foreach name=outer key=key item=item from=$pages.type}
{if $item == 'offer'}
{include file="offer.tpl" offer=????}
{/if}
{/foreach}
|
[php:1:f59057e0eb]<?php
$smarty -> assign('offerxx0',$offer1);
$smarty -> assign('offerxx1',$offer2);
?>[/php:1:f59057e0eb]
In this example variables offerxx0 and offerxx1 are visible in foreach loop but I have no idea how to pass them to the included file using a "key".
Thanks |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Jul 16, 2003 11:44 pm Post subject: |
|
|
hmm. When you are assigning the array, you should assign the whole thing, not the individual elements. In fact, when you do this:
$smarty -> assign('offerxx[0]',$offer1);
$smarty -> assign('offerxx[1]',$offer2);
Smarty does not make a template var called 'offerxx' as an array with two elements ([0] and [1]) but instead, it creates TWO variables, the first named 'offerxx[0]' and the second, 'offerxx[1]'. That's why you can't access the "offerxx array" -- it doesn't exist.
Perhaps if you did something like this?
$offerx = array( 0=>$offer1, 1=>$offer2 );
$smarty->assign('offerxx', $offerx);
Now Smarty assigns the template var 'offerxx' to the array offerx which has the structure you seem to be expecting.
HTH |
|
Back to top |
|
malou Smarty n00b
Joined: 15 Jul 2003 Posts: 3
|
Posted: Thu Jul 17, 2003 11:42 am Post subject: |
|
|
boots wrote: | Perhaps if you did something like this?
$offerx = array( 0=>$offer1, 1=>$offer2 );
$smarty->assign('offerxx', $offerx);
|
Thanks... now it works perfectly |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|