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

Best way to do: first last odd even

 
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
balupton
Smarty Rookie


Joined: 15 Feb 2008
Posts: 11
Location: Perth, Australia

PostPosted: Sat Mar 29, 2008 7:34 pm    Post subject: Best way to do: first last odd even Reply with quote

Has anyone come up with a better way to do the css class names for first, last, odd and even?

Here is what I have come up with:
Code:
      <ul class="header inline menu">
      {foreach from=$menu_items item=i name=menu_items}
         {cycle values='odd,even' assign='class'}
         {if $smarty.foreach.menu_items.first}
            {assign var='class' value=$class|cat:' first'}
         {elseif $smarty.foreach.menu_items.last}
            {assign var='class' value=$class|cat:' last'}
         {/if}
         <li class="{$class}"><a href="index.php?page={$i.code}">{$i.title}</a></li>
      {/foreach}
      </ul>


Surely there must be an simpler way of doing this? Like an inbuilt thing into smarty's foreach function...


Last edited by balupton on Sat Mar 29, 2008 8:56 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
U.Tews
Administrator


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

PostPosted: Sat Mar 29, 2008 8:26 pm    Post subject: Reply with quote

I think there is no better way.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Sun Mar 30, 2008 12:11 am    Post subject: Reply with quote

Here is a way to get rid of the assign, thats about it, aside from assigning class values in PHP to the $menu_items array elements.

Code:
<ul class="header inline menu">
      {foreach from=$menu_items item=i name=menu_items}
         {if $smarty.foreach.menu_items.first}
            <li class="{cycle values='odd,even'} first">
         {elseif $smarty.foreach.menu_items.last}
            <li class="{cycle values='odd,even'} last">
         {else}
            <li class="{cycle values='odd,even'}">
         {/if}
         <a href="index.php?page={$i.code}">{$i.title}</a></li>
      {/foreach}
</ul>
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


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

PostPosted: Sun Mar 30, 2008 12:16 am    Post subject: Reply with quote

Another option to cleanup the code, write a custom function to create your class name, passing it the foreach index and the array size:
Code:

      <ul class="header inline menu">
      {foreach from=$menu_items item=i name=menu_items}
         <li class="{getclass index=$smarty.foreach.menu_items.index total=$menu_items|@count}"><a href="index.php?page={$i.code}">{$i.title}</a></li>
      {/foreach}
      </ul>
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