Smarty to LINE-BREAK when write into FILE?

白昼怎懂夜的黑 提交于 2019-12-23 04:27:03

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/21952796/smarty-to-line-break-when-write-into-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!