php – symfony 1.4解释并且不会在模板中使用fetchOne()显示html标签

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – symfony 1.4解释并且不会在模板中使用fetchOne()显示html标签脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下设置:PHP 5.4,symfony 1.4.17和firefox,即chrome.

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

>表:TbNews
>列:

> id作为主键
> scontent作为保存新闻内容的文本字段.它将内置htML内容,与CKedITor一起保存,并且完美运行.

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

如果我使用symfony pager(在行动中,然后是模板),则不会解释html,并且我会在输出中看到带有内容HTML标记.
你可以看到下面的例子,它们显示我正在谈论的内容.

我已阅读其他主题,出于安全原因,symfony输出escaPEr会自动将HTML转换为“text”,我们必须对数据使用getRawValue来获取原始HTML字符.

> show html tags in template – symfony and CKEDITOR. how safety?
> Stop symfony from escaping html from query result
> Symfony.com View Layer Output Escaping
> Symfony sfOutputEscaper method getRawValue()

我有一些问题:

>为什么symfony输出escaper正在使用symfony pager及其
如果我们使用fetchOne()不工作?
>我应该如何使用symfony在下面的示例中使用getRawValue()
寻呼机,解释HTML,然后只显示内容
> 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']; // &amp;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.

解决方法

你的第一次测试是错误的.

当你使用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

如果您已激活escaping_strategy&在您的应用程序/ [app_name] /config/settings.yml中的escaping_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'];
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – symfony 1.4解释并且不会在模板中使用fetchOne()显示html标签全部内容,希望文章能够帮你解决php – symfony 1.4解释并且不会在模板中使用fetchOne()显示html标签所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。