写入FILE时聪明到LINE-BREAK吗? [英] Smarty to LINE-BREAK when write into FILE?

查看:67
本文介绍了写入FILE时聪明到LINE-BREAK吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个Smarty Loop,例如:

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}

.. 我期望的来源:

Apple,
Banana,
Candy

..写为文件.

我的PHP代码(用于写文件)是:

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

即使我在Smarty tpl中使用\r\r\n,它们也只是作为字符本身被打印出来.
始终不打破界限.

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.

请怎么做?

推荐答案

来自 {strip} 在您的其他问题中我引用了 (并要求您阅读):

{strip}{/strip}标记中的所有内容都去除了或回车符处的多余空格 在显示行之前和之后的行的开头和结尾.这样你可以保持你的 模板可读,不必担心多余的空格会引起问题.

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.

一种解决方案:

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

将条带去除空白,并用文字力,<newline>作为分隔符.注意:{literal}{/literal}

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

(已通过smarty 3.1.16测试)

(tested with smarty 3.1.16)

这篇关于写入FILE时聪明到LINE-BREAK吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆