Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Dedicated server web hosting provided by Guru-host.eu.
Insert div in a foreach loop

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
m4nux
Smarty n00b


Joined: 12 Aug 2012
Posts: 3

PostPosted: Sun Aug 12, 2012 9:06 pm    Post subject: Insert div in a foreach loop Reply with quote

Hi there,
I'm trying to find how to insert a div between the 2nd and 3rd element of a foreach loop. How would that be possible? Here's my code:
Code:

    {foreach from=$listings item=listing name=listings_block}
        <div class="job margin-bottom {cycle values = 'evenrow,oddrow' advance=true}" itemscope itemtype="http://schema.org/JobPosting">
            {if $listing.user.Logo.thumb_file_url}
                    <div class="grid_4 alpha featured-jobs-employers-logo">
                            <a class="employer_logo_centered" href="{$GLOBALS.site_url}/display-job/{$listing.id}/{$listing.Title|regex_replace:"/[\\/\\\:*?\"<>|%#$\s]/":"-"}.html" itemprop="URL"><img itemprop="image" src="{$listing.user.Logo.thumb_file_url}" alt="" /></a>
                    </div>
            {/if}
            <h2 class="grid_6 featured-description-title" itemprop="title"><a href="{$GLOBALS.site_url}/display-job/{$listing.id}/{$listing.Title|regex_replace:"/[\\/\\\:*?\"<>|%#$\s]/":"-"}.html">{$listing.Title|strip_tags|truncate:45}</a></h2>   
            <div class="featured-buttons grid_5 omega ">
                <a class="featured-button featured-button-green" href="{$GLOBALS.site_url}/display-job/{$listing.id}/{$listing.Title|regex_replace:"/[\\/\\\:*?\"<>|%#$\s]/":"-"}.html">View</a>
                <a class="featured-button" href="{$GLOBALS.site_url}/saved-ads/?listing_id={$listing.id}" onclick="popUpWindow('{$GLOBALS.site_url}/saved-ads/?listing_id={$listing.id}&displayForm=1', 800, 450, '[[Save this Job]]', true, {if $GLOBALS.current_user.logged_in}true{else}false{/if}); return false;">Save</a>
                <a class="featured-button" href="{$GLOBALS.site_url}/tell-friends/?listing_id={$listing.id}" onclick="popUpWindow('{$GLOBALS.site_url}/tell-friends/?listing_id={$listing.id}', 500, 360, '[[Tell a Friend]]'); return false;">[[Tell a Friend]]</a>
            </div>
            <div class="featured-description grid_11">      
                <p>{if $listing.company_name}<a href="{$GLOBALS.site_url}/search-results-jobs/?action=search&amp;username[equal]={$listing.company_name}"><span itemprop="hiringOrganization" itemscope itemtype="http://schema.org/Organization"><span itemprop="name">{$listing.company_name}</span></span></a>{else}<a href="{$GLOBALS.site_url}/company/{$listing.user.id}/{$listing.user.CompanyName|replace:" ":"-"|escape:"url"}"><span itemprop="hiringOrganization" itemscope itemtype="http://schema.org/Organization"><span itemprop="name">{$listing.user.CompanyName}</span></span></a>{/if},
                {if $listing.City}<span itemprop="jobLocation" itemscope itemtype="http://schema.org/Place"><span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"><span itemprop="addressLocality">{$listing.City}</span></span></span>, {/if}[[$listing.Country]]</p>
            </div>
            <div class="grid_11">
                <a href="#" class="toggleLink">Description</a>   
                <div class="toggle grid_15 pull_4">
                <br />
                <h4>Job Description</h4>
                    <div class="margin-bottom">
                        {$listing.JobDescription}
                    </div>
                    <div class="button-job-description">
                        <a class="featured-button featured-button-green" href="{$GLOBALS.site_url}/display-job/{$listing.id}/{$listing.Title|regex_replace:"/[\\/\\\:*?\"<>|%#$\s]/":"-"}.html">View full job description</a>
                    </div>                                   
                </div>
            </div>
        </div>
    {/foreach}

Anyone can help?
Thanks!
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 521

PostPosted: Sun Aug 12, 2012 11:59 pm    Post subject: Re: Insert div in a foreach loop Reply with quote

Code:


   {foreach from=$listings item=listing name=listings_block}

      {if $smarty.foreach.listings_block.iteration == 3}
        <div> stuff that goes after 2nd iteration here</div>
      {/if}

      your other stuff here

   {/foreach}




works if there are at least 3 iterations. is that what you mean?
Back to top
View user's profile Send private message
m4nux
Smarty n00b


Joined: 12 Aug 2012
Posts: 3

PostPosted: Mon Aug 13, 2012 7:25 am    Post subject: Yep Reply with quote

Thanks, that was it! it works like a charm Very Happy
Back to top
View user's profile Send private message
m4nux
Smarty n00b


Joined: 12 Aug 2012
Posts: 3

PostPosted: Mon Aug 13, 2012 7:28 am    Post subject: Another thing Reply with quote

Just curiosity,
Could you as well tell me how to insert the thing every 3 listing?
Thanks again
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 698
Location: Germany, border to Switzerland

PostPosted: Mon Aug 13, 2012 7:46 am    Post subject: Reply with quote

Code:
{foreach from=$data item=item name=foobar}
  {if $smarty.foreach.foobar.iteration is div by 3}
    <div> a div every 3 elements </div>
  {/if}
{/foreach}

_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    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