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

loop-problem - seperator between elements

 
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
andreas
Smarty Pro


Joined: 22 Apr 2003
Posts: 106

PostPosted: Sat Apr 26, 2003 11:15 am    Post subject: loop-problem - seperator between elements Reply with quote

Hi!

I think it should be a common problem, so perhaps someone here could help me.

I want to build a link-list with smarty, it should look like that:

<a href="link1.htm">link1</a>&|&<a href="link2.htm">link2</a>&|&<a href="link3.htm">link3</a>


What I have in PHP is:

array(array('url' =>'link1.htm', 'name' => 'link1'), array('url' =>'link2.htm', 'name' => 'link2'))

Thats what I have. I thought about assigning this array to my template, and then try something like this:

{foreach from=$navigation item=nav}
<a href="{$nav.url}">{$nav.name}</a>&|&
{/foreach}

Thats nice, but I will have a "&|&" in the end, but this should be a seperator only between the links. foreachelse is no solution I think.

In PHP I could solve this by using for, but thats not nice, in PHP perhaps I would use something like implode... but what is the way to do this with smarty?

bye
Andreas


Last edited by andreas on Wed May 05, 2010 10:33 pm; edited 1 time in total
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sat Apr 26, 2003 12:17 pm    Post subject: Reply with quote

just like a section, you can give your foreach a name and access it's
properties via $smarty:

{foreach name=nav from=$navigation item=nav}
<a href="{$nav.url}">{$nav.name}</a>
{if !$smarty.foreach.nav.last}&|&{/if}
{/foreach}
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Sat Apr 26, 2003 12:18 pm    Post subject: Re: loop-problem - seperator between elements Reply with quote

Code:

{foreach from=$navigation item="nav" name="nav"}
   <a href="{$nav.url}">{$nav.name}</a>{if (!$smarty.foreach.nav.last)}&|&{/if}
{/foreach}


and I say again... this is NOT documented... HELLLOOOOOO... :S

(andreas, note that it's the same like in {section}s, where you can give sections names and then access their properties... does work the same way in foreaches)
Back to top
View user's profile Send private message
Vynka
Smarty Rookie


Joined: 30 Apr 2003
Posts: 17
Location: Belgium

PostPosted: Wed Apr 30, 2003 7:53 am    Post subject: Section and foreach have same properties ?! Reply with quote

Hello!

Idea If you want get the current interation with foreach, you can use the iteration propertie like in section:

Code:
{foreach name=datas intem=col from=$myArray}
    {if $smarty.foreach.datas.first}<hr>{/if}
    {$smarty.foreach.datas.iteration} - {$col}
    {if !$smarty.foreach.datas.last}<br>{/if}
{/foreach}


I think the three properties avaible with foreach are : first; last; iteration
_________________
Au pays des Gloups!
http://www.wishlist.lu
Back to top
View user's profile Send private message Visit poster's website
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