سؤال

I have a Smarty Loop as below, for e.g:

{strip}
    {assign "comma" ""}
    {foreach from=$List item=Item}
        {$comma}{$Item.title}
        {assign "comma" ","}
    {/foreach}
{/strip}

.. from which I EXPECT is:

Apple,
Banana,
Candy

.. WRITTEN as a FILE.

My PHP codes (to write the file) are:

$f = fopen('myfile.txt', 'w');
fwrite( $f, $smarty->fetch('sample.tpl') );

But instead IN REALITY, it is being written as like below:

Apple,Banana,Candy

Even if i use \r or \r\n in Smarty tpl, they are just being printed out, as the characters themselfs.
Not breaking the lines anyway.

How to do it please?

هل كانت مفيدة؟

المحلول

From the doc of {strip} I quoted in your other question (and asked you to read):

Anything within {strip}{/strip} tags are stripped of the extra spaces or carriage returns at the beginnings and ends of the lines before they are displayed. This way you can keep your templates readable, and not worry about extra white space causing problems.

One solution:

{strip}
    {foreach name=foo from=$List item=Item}
            {$Item.title}{if !$smarty.foreach.foo.last}{literal},
{/literal}{/if}
    {/foreach}
{/strip}

Strip remove blanks and literal force ,<newline> to be the separator. NB: spaces and newline are meaningful between {literal} and {/literal}

(tested with smarty 3.1.16)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top