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

Smarty 3: Foreach Examples

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
SoN9ne
Smarty Rookie


Joined: 25 Feb 2010
Posts: 17

PostPosted: Fri Mar 26, 2010 3:00 pm    Post subject: Smarty 3: Foreach Examples Reply with quote

Since I am new to Smarty, (Well, not really new, just haven't used it in a few years) I wanted to see some examples of foreach loops being used in the template.

Checking the manual still left me with a few questions.

I thought I'd share a few foreach examples to help other users, like me, that learn better by example.

This method of foreach is possible, asides from the method listed in the manual (I believe most know this but better to be safe than sorry)
Code:
{foreach $res as $k}
 {$k}
{/foreach}


This method is possible too
Code:
{foreach $res as $k=>$v}
 {$k} => {$v['name']}
{/foreach}


Below are some examples I use
Nav building examples:
Code:
      <div id="navigation">
         <ol>
            {foreach $links as $navLink}
               {if $navLink['link']}
                  <li><a href="{$navLink['link']}" title="{$navLink['name']}">{$navLink['name']}</a></li>
               {else}
                  <li>|</li>   
               {/if}
            {/foreach}      
         </ol>
      </div>


Meta building example:
Code:
{if $SYSTEM_HEADER_DATA['meta']}
   {foreach $SYSTEM_HEADER_DATA['meta'] as $mItem}
      {if $mItem}
         <meta {if $mItem['name']}name="{$mItem['name']}"{/if} {if $mItem['http-equiv']}http-equiv="{$mItem['http-equiv']}"{/if} {if $mItem['content']}content="{$mItem['content']}"{/if} {if $mItem['scheme']}scheme="{$mItem['scheme']}"{/if} />
      {/if}
   {/foreach}
{/if}   


Example of building header data:
Code:
{if $SYSTEM_HEADER_DATA}
   {foreach $SYSTEM_HEADER_DATA as $hMethod=>$hObj}
      {switch $hMethod}
         {case 'js'}
            {if $hObj}
               {foreach $hObj as $hItem}
               {if $hItem}
               <script type="text/javascript" src="{$TEMPLATE_JS}{$hItem}.js"></script>
               {/if}
               {/foreach}
            {/if}      
         {break}   
      
         {case 'js-inline'}
            {if $hObj}
               <script>
               <!--
               {foreach $hObj as $hItem}
               {$hItem}
               {/foreach}
               // -->
               </script>
            {/if}      
         {break}
         
         {case 'css'}
            {if $hObj}
               {foreach $hObj as $hItem}
               {if $hItem}
               <link type="text/css" href="{$TEMPLATE_CSS}{$hItem}.css" rel="stylesheet" />
               {/if}
               {/foreach}
            {/if}      
         {break}
         
         {case 'css-inline'}
            {if $hObj}
               <style>
               {foreach $hObj as $hItem}
               {$hItem}
               {/foreach}
               </style>   
            {/if}      
         {break}
      {/switch}   
   {/foreach}
{/if}


The switch used in the above example is a plugin from http://pynej.blogspot.com/2010/02/switch-statment-for-smarty-3.html
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 -> Tips and Tricks 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