|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
clone46 Smarty Rookie
Joined: 13 Jan 2006 Posts: 15
|
Posted: Wed Jun 18, 2014 11:09 pm Post subject: Advice for delimiters between optionally visible items |
|
|
Hello,
I have a situation that looks something like this:
Code: |
{if $foo != '1234'}
{$foo},
{/if}
{if $bar != '54'}
{$bar},
{/if}
{if $beer != "yum"}
{$beer}
{/if}
|
As you can probably foresee, if $beer == "yum" (which is pretty often), there's an extra comma at the end of the list. Is there any graceful way to handle this?
Thanks,
Bret |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Jun 20, 2014 12:35 pm Post subject: |
|
|
Something like this
Code: |
{values = []}
{if $foo != '1234'}
{$values[] = $foo}
{/if}
{if $bar != '54'}
{$values[] = bar}
{/if}
{if $beer != "yum"}
{$values[] = $beer}
{/if}
[code]{foreach $values as $val}{if $val@index > 0}, {/if}{$val}{/foreach}[/code]
writing the {foreach} in one line should put the spaces at the light place. |
|
|
Back to top |
|
|
|
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
|