【问题标题】:Twig nl2br filter not working on entityTwig nl2br 过滤器不适用于实体
【发布时间】:2012-07-20 14:27:05
【问题描述】:

我有一个带有 __toString() 方法的地址实体,如下所示:

public function __toString() {
    $result = $this->getStreet();

    if ($this->getStreet2())
        $result .= '\n' . $this->getStreet2();

    $result .= '\n' . $this->getZipCode().' '.$this->getCity();
    return $result;
}

在我的模板中,我将Twig nl2br filter 应用于实体:

{{ user.address|nl2br }}   

但我仍然得到转义的 \n :

1107 West Adams Boulevard\n90007 Los Angeles, CA

我尝试使用这个字符串而不是实体:

{{ "1107 West Adams Boulevard\n90007 Los Angeles, CA"|nl2br }}   

我得到了预期的结果:

西亚当斯大道 1107 号

90007 洛杉矶,加利福尼亚

我也试过

{{ user.address|raw|nl2br }} 

这是不安全的,但仍然不起作用...... 我试过 Twig 1.8.0 和 1.9.0。

有什么想法吗?

【问题讨论】:

  • 使用"\n" 而不是'\n' :)

标签: php symfony twig template-engine nl2br


【解决方案1】:

而不是:

$result .= '\n' . $this->getZipCode().' '.$this->getCity();

试试这个:

$result .= "\n" . $this->getZipCode()." ".$this->getCity();

PHP 将解释双引号字符串中的转义字符。

This PHP Manual page 可能会有所帮助。

【讨论】:

  • 我不知道!感谢您的回答和链接
猜你喜欢
  • 1970-01-01
  • 2012-06-03
  • 2013-11-30
  • 2013-04-29
  • 2014-03-11
  • 2021-07-07
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多