Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Calling 2 templates

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
heohni
Smarty Regular


Joined: 20 Sep 2006
Posts: 40

PostPosted: Sun Nov 11, 2007 8:17 pm    Post subject: Calling 2 templates Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sun Nov 11, 2007 8:43 pm    Post subject: Reply with quote

The template looks okay for me, Have you checked the content of $array?
Back to top
View user's profile Send private message
scuzzy
Smarty Regular


Joined: 31 Aug 2003
Posts: 84

PostPosted: Mon Nov 12, 2007 1:42 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
heohni
Smarty Regular


Joined: 20 Sep 2006
Posts: 40

PostPosted: Mon Nov 12, 2007 8:31 am    Post subject: Reply with quote

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
View user's profile Send private message
heohni
Smarty Regular


Joined: 20 Sep 2006
Posts: 40

PostPosted: Tue Nov 13, 2007 7:50 am    Post subject: Reply with quote

no ideas?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Nov 13, 2007 3:05 pm    Post subject: Reply with quote

Try printing {$artikel[x].bereich} in the loop, it may not be what you expect.
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Nov 13, 2007 6:35 pm    Post subject: Reply with quote

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
View user's profile Send private message
heohni
Smarty Regular


Joined: 20 Sep 2006
Posts: 40

PostPosted: Wed Nov 14, 2007 9:50 am    Post subject: Reply with quote

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
View user's profile Send private message
heohni
Smarty Regular


Joined: 20 Sep 2006
Posts: 40

PostPosted: Wed Nov 14, 2007 9:54 am    Post subject: Reply with quote

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

Code:

{$___content1}


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 Rolling Eyes
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP