【问题标题】:symfony 1.4 interprets and doesnt show html tags using fetchOne() in templatesymfony 1.4 在模板中使用 fetchOne() 解释并且不显示 html 标签
【发布时间】:2013-02-01 11:59:30
【问题描述】:

我有以下设置:php 5.4、symfony 1.4.17 和 firefox,即 chrome。

我已经构建了一个简单的新闻模块。

  • 表:TbNews
  • 列:
    • id 作为主键
    • scontent 作为保存新闻内容的文本字段。它将包含 html 内容,使用 CKeditor 保存,并且可以完美运行。

如果我使用fetchOne()(在模板中),则在写入内容之前会解释 html。

如果我使用 symfony 寻呼机(在行动中,然后是模板),则不会解释 html,并且我会在输出中看到带有内容的 HTML 标记。 您可以查看下面的示例,这些示例准确地显示了我在说什么。

我在其他主题中读到,出于安全原因,symfony 输出转义器会自动将 HTML 转换为“文本”,我们必须在数据上使用 getRawValue 来获取原始 HTML 字符。

我有一些问题:

  1. 为什么 symfony 输出转义器与 symfony 寻呼机一起工作,及其 如果我们使用fetchOne() 则不起作用?
  2. 我应该如何在下面的示例中使用getRawValue() 和 symfony 寻呼机,解释 HTML,然后只显示内容?
  3. getRawValue() 是仅获取书面内容的最佳选择吗?

示例代码:

//1. fetchOne() outputs content interpreting html before.
//index/templates/indexSuccess.php
//-------------------------------
$q = Doctrine_Query::create()->from('TbNews e')->where('e.id = ?', '1');
$new = $q->fetchOne(); // <p>testcontent</p>\r\n
echo $new['scontent']; 
// output:  testcontent  --- OK, output is not escaped because we are jumping symfony output escaper since we are doing it directly in the action.


//2. Get all news with symfony pager, html tags are not interpreted, html tags are shown.
//index/actions/actions.class.php
//-------------------------------
$app_max_news_in_homepage = 4;
$this->pager = new sfDoctrinePager('TbNews', $app_max_news_in_homepage);
$this->pager->setQuery(Doctrine::getTable('TbNews')->createQuery('a'));
$this->pager->setPage($request->getParameter('page', 1));
$this->pager->init();

//index/templates/indexSuccess.php 
//--------------------------------
foreach ($pager->getResults() as $new)
{
  echo $new['scontent']; // &lt;p&gt;testcontent&lt;/p&gt;\r\n
}

//output:  <p>testcontent</p>  --- OK, since output is escaped by symfony output escaper since we get data at the action and show it in the template.

【问题讨论】:

    标签: php html escaping symfony-1.4 html-escape-characters


    【解决方案1】:

    你的第一个测试错了。

    当您使用fetchOne() 进行测试时,您处于一个动作中。因此,您从数据库中检索的内容和您显示的内容(使用echo)不会被转义,因为它没有发送到模板。

    当您执行第二个测试时,您从操作中检索内容并在模板中显示结果。在这种情况下,内容被sfOutputEscaper 转义。如果您进行第一次测试,然后尝试在模板中显示内容,您会看到 html 已转义。

    // in actions
    $this->new = $q->fetchOne();
    
    // in template
    echo $new['scontent'];
    
    // result-> &lt;p&gt;testcontent&lt;/p&gt;\r\n
    

    如果您在apps/[app_name]/config/settings.yml 中激活了escaping_strategyescaping_method,则将转义给模板的所有内容。

    当我想显示一个被转义的html内容时,我通常使用sfOutputEscaper中的unescape方法。在你的情况下:

    foreach ($pager->getResults() as $new)
    {
      echo sfOutputEscaper::unescape($new['scontent']);
    }
    

    另一种选择(Michal Trojanowski 表示):

    foreach ($pager->getResults()->getRawValue() as $new)
    {
      echo $new['scontent'];
    }
    

    【讨论】:

    • 非常感谢您的回答 j0k,阅读速度会很慢,但我认为您解决了我的问题 :)
    • sfOutputEscaper 也有一个方法 getRawValues() 在这里很有用:) 尝试$pager-&gt;getResults()-&gt;getRawValues() 然后echo $new['content'] 将显示未转义的值。
    • 感谢两位的回答。是的,您对我的错误是完全正确的,因为在我的第一个 fetchOne() 代码中,我直接在模板中使用它,而不是从操作中调用它(在我的帖子中添加了该信息)。正如你所展示的,如果从动作中直接完成,它也应该被转义。再测试一下并添加您的解决方案,看看它是否有效,ty :)
    • @MichalTrojanowski 我不知道getRawValues() 谢谢:) edit 你在哪里看到这个方法?我找不到它。
    • 刚刚测试过:getRawValue()。没有最后的's' - $pager->getResults()->getRawValue() as $new symfony-project.org/api/1_4/sfOutputEscaper#method_getrawvalue
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 2012-10-28
    • 1970-01-01
    相关资源
    最近更新 更多