|
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 |
andreas Smarty Pro
Joined: 22 Apr 2003 Posts: 106
|
Posted: Sat Apr 26, 2003 11:15 am Post subject: loop-problem - seperator between elements |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sat Apr 26, 2003 12:17 pm Post subject: |
|
|
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 |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Sat Apr 26, 2003 12:18 pm Post subject: Re: loop-problem - seperator between elements |
|
|
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 |
|
Vynka Smarty Rookie
Joined: 30 Apr 2003 Posts: 17 Location: Belgium
|
Posted: Wed Apr 30, 2003 7:53 am Post subject: Section and foreach have same properties ?! |
|
|
Hello!
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 |
|
|
|
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
|