View previous topic :: View next topic |
Author |
Message |
heohni Smarty Regular
Joined: 20 Sep 2006 Posts: 40
|
Posted: Sun Nov 11, 2007 8:17 pm Post subject: Calling 2 templates |
|
|
Hi,
for me a difficult case:
I have - lets say- an array with 4 data elements. The first 2 items I want to parse into layout1.tpl, the last 2 items in template layout2.tpl. To recognize the point when changing the templates, I have a variable ("bereich") in my array which gives me this information. The array is sorted by this number, so there is only one time the point when I need to change the template.
Like:
Code: | $smarty->assign('artikel', $array); |
Code: |
{section name="x" loop=$artikel}
{if $artikel[x].bereich == 1}
{include file="layout1.tpl"}
{elseif $artikel[x].bereich == 2}
{include file="layout2.tpl"}
{/if}
{/section}
|
The problem is, that I get all 4 elements parsed in the first template.
Any ideas how I could solve this problem?
Thanks for every good idea!![/code] |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun Nov 11, 2007 8:43 pm Post subject: |
|
|
The template looks okay for me, Have you checked the content of $array? |
|
Back to top |
|
scuzzy Smarty Regular
Joined: 31 Aug 2003 Posts: 84
|
Posted: Mon Nov 12, 2007 1:42 am Post subject: |
|
|
Try changing
Code: | {if $artikel[x].bereich == 1} |
to
Code: | {if $artikel[x].bereich === 1} |
or if the value is a string
Code: | {if $artikel[x].bereich === '1'} |
At the moment, it is probably just doing a true comparison, and a string of 2 can also be matched as true. |
|
Back to top |
|
heohni Smarty Regular
Joined: 20 Sep 2006 Posts: 40
|
Posted: Mon Nov 12, 2007 8:31 am Post subject: |
|
|
Hi,
the array is ok, as every data is well displayed with the one template.
I tried out the ===1 and ===2 but then I don't get output at all!
I could seperate my array into 2 if needed. But How I can use my templates in that case?
Infact I don't want to create a copy of layout1 and layout2.tpl for this special case... |
|
Back to top |
|
heohni Smarty Regular
Joined: 20 Sep 2006 Posts: 40
|
Posted: Tue Nov 13, 2007 7:50 am Post subject: |
|
|
no ideas? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Nov 13, 2007 3:05 pm Post subject: |
|
|
Try printing {$artikel[x].bereich} in the loop, it may not be what you expect. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Nov 13, 2007 6:35 pm Post subject: |
|
|
A better idea is to split the array into two, perhaps using a custom plugin or maybe better, have your calling script send two arrays since it is already doing the work of sorting and providing the somewhat superfluous switch field on every record.
Another idea is to use {foreach} rather than {section}. That's just a general thing I suggest unless {section} is the only possibility (which is extremely rare). |
|
Back to top |
|
heohni Smarty Regular
Joined: 20 Sep 2006 Posts: 40
|
Posted: Wed Nov 14, 2007 9:50 am Post subject: |
|
|
mohrt wrote: | Try printing {$artikel[x].bereich} in the loop, it may not be what you expect. |
That's not the problem, it is the figure I want. |
|
Back to top |
|
heohni Smarty Regular
Joined: 20 Sep 2006 Posts: 40
|
Posted: Wed Nov 14, 2007 9:54 am Post subject: |
|
|
boots wrote: | A better idea is to split the array into two, perhaps using a custom plugin or maybe better, have your calling script send two arrays since it is already doing the work of sorting and providing the somewhat superfluous switch field on every record.
Another idea is to use {foreach} rather than {section}. That's just a general thing I suggest unless {section} is the only possibility (which is extremely rare). |
As it happen meanwhile that I have to extend the "bereich" area for different templates I created the following solution:
Inside a foreach I've put a switch where I check for every case.
Inside the case
Code: |
case 1:
$standard[] = $artikel[$key];
$smarty->assign('artikel1', $standard);
$smarty->assign( '___content1', $smarty->fetch( '4.tpl' ) );
break;
|
I create a new array - just with the found data - and use fetch to put the output into a new variable.
Then on a new template file I call
to output the results.
Until jet, it wokrs fine for me.
I don't know if that is the best way doing so, but as far as I can see it works. Live could be easier if I would not have such a complex array structure. But if it would be easy it would be boring, or |
|
Back to top |
|
|