Search code examples
phpforeachsmartytrim

Smarty remove trailing comma in foreach loop


I have the following loop in smarty:

{foreach from=$method.params key=name item=type}
    <i style="font-weight: normal;">{$type}</i> {$name},
{/foreach}

What is the best way to remove the trailing comma?


Solution

  • I prefer Twig over Smarty so I'm working from the docs, but here's my stab:

    {assign var="comma" value=""}
    {foreach from=$method.params key=name item=type}
        {$comma}<i style="font-weight: normal;">{$type}</i> {$name}
        {assign var="comma" value=", "}
    {/foreach}