Вопрос

I've following logic to print strings. Now the strings are getting printed but they are not separated by comma. I want to insert comma separator after each string printed. The comma should not get printed ater last string. How to achieve this? I tried almost every thing but not getting the desired result. So can someone who has good command over smarty help me in this regard? Thanks in advance.

{foreach from=$all_states item=state key='key'}
  {foreach from=$preview_data.applicable_states item=pre key='index'}    
    {if $state.id == $pre} {$state.state_name} {/if}
  {/foreach}  
{/foreach}
Это было полезно?

Решение

Another way to look at the problem is that a comma should be printed before every string except the 1st one, so create a variable called "comma" and initialize it to the empty string and set it to "," after the first string is printed.

Not tested but you get the idea:

{assign var="comma" value=""}
{foreach from=$all_states item=state key='key'}
  {foreach from=$preview_data.applicable_states item=pre key='index'}    
    {if $state.id == $pre} {$comma}{$state.state_name} {assign var="comma" value=","}{/if}
  {/foreach}  
{/foreach}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top