Page 1 of 1

Need help with {foreach} in Album module

Posted: Sat Sep 05, 2009 1:57 pm
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.

Re: Need help with {foreach} in Album module

Posted: Sat Sep 05, 2009 2:44 pm
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}
);