Need help with {foreach} in Album module

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
paddy
New Member
New Member
Posts: 4
Joined: Fri Aug 15, 2008 7:42 pm

Need help with {foreach} in Album module

Post by paddy »

How I can detect the last image in a foreach loop? I need the module to spit out a slighly different piece of code for the last image. It`s for a jQuery preloader (used in conjunction with jCarousel).

Below is the code I need to generate. You can see that the last image can`t have a comma after it, otherwise it won`t validate.

Code: Select all

$.preloadImages("image1.jpg", image2.jpg","image3.jpg");
Many thanks.
User avatar
plger
Forum Members
Forum Members
Posts: 196
Joined: Wed Oct 15, 2008 10:38 am

Re: Need help with {foreach} in Album module

Post by plger »

Did you search?
What you are looking for can be found easily on the smarty website :
http://www.smarty.net/manual/en/language.function.foreach.php#foreach.property.last
You give a name to your loop and retrieve the boolean .last attribute:

Code: Select all

{strip}
$.preloadImages(
{foreach from=$whatever item="item" name="myloop"}
   "{$item->variable}"
   {if !$smarty.foreach.myloop.last},{/if}
{/foreach}
{/strip}
);
But even if you had never heard about this, you could have done it in countless other ways...

Code: Select all

{strip}
$.preloadImages(
{assign var="isfirst" value=1}
{foreach from=$whatever item="item"}
   {if $isfirst}{assign var="isfirst" value=0}{else},{/if}
   "{$item->variable}"
{/foreach}
{/strip}
);

Code: Select all

{strip}
$.preloadImages(
{foreach from=$whatever item="item" key="key"}
   {if $key>0},{/if}
   "{$item->variable}"
{/foreach}
{/strip}
);
Post Reply

Return to “Modules/Add-Ons”