Smarty prints a WHITE SPACE between variables (in

2019-06-14 13:00发布

问题:

Assigning from PHP:

$smarty->assign("myArrays", Array(
                   Array( "title" => "ABC", "whatever" => 45),
                   Array( "title" => "DEF", "whatever" => 78)
               ));

In Smarty (v3.1.16) .tpl file:

{assign "seperator" "|"}
{foreach from=$myArrays item=currentItem}
    {$seperator}{$currentItem.title}{$seperator}
{/foreach}

Then it will output as:

|ABC| |DEF|

.. WITH A "SPACE" in-between.
And i think it is only in such LOOPS.

Why is so?
And how to solve it please?

回答1:

Use no spaces in the loop:

{foreach from=$myArrays item=currentItem}{$seperator}{$currentItem.title}{$seperator}{/foreach}

or use the smarty directive to make smarty remove spaces: {strip}/{strip}:

{strip}
    {foreach from=$myArrays item=currentItem}
        {$seperator}{$currentItem.title}{$seperator}{/foreach}
    {/foreach}
{/strip}