我如何避免在 CakePHP 的视图中出现 html 个实体?
How do I avoid html entities in the view in CakePHP?
所以我的数据被保存在 mysql 中,带有实际的撇号等。但是当 CakePHP 像这样在视图中吐出它时:If you're on this page
我不想对应用程序中的每个字符串执行 htmlspecialchars_decode()
。是否有针对此类问题的 CakePHP 解决方案?
这里是正文的回显:
<?php echo $this->Text->autoLinkUrls(h($item['Item']['post_comment'])); ?>
这是 post_comment 的字段信息:
这是提交的表单字段:
echo $this->Form->textarea('post_comment', array('label' => false, 'placeholder' => 'Ask a question or post a link', 'rows' => '3', 'class'=> 'u-full-width'));
编辑:
更奇怪的是 - 在应用程序的其他部分,撇号显示得很好。
autoLinkUrls
采用与 link()
相同的选项。所以它可以帮助。试试 -
<?php echo $this->Text->autoLinkUrls(h($item['Item']['post_comment']), array('escape' => false)); ?>
I don't want to have to do htmlspecialchars_decode() on every string in my app. Is there a CakePHP solution for this kind of issue?
是的,您需要了解,通过 CakePHP 核心帮助程序传递的所有内容在渲染之前都会使用 CakePHP 的 h()
函数进行内部清理,这是 htmlspecialchars()
的一个便利函数。 请注意,第三方助手可能不会那样做!所以请检查一下。
我认为几乎所有的辅助方法都有禁用转义的选项:
$this->Helper->method($foo, ['escape' => false]);
确保在禁用时不会意外允许输出恶意字符串。
所以我的数据被保存在 mysql 中,带有实际的撇号等。但是当 CakePHP 像这样在视图中吐出它时:If you're on this page
我不想对应用程序中的每个字符串执行 htmlspecialchars_decode()
。是否有针对此类问题的 CakePHP 解决方案?
这里是正文的回显:
<?php echo $this->Text->autoLinkUrls(h($item['Item']['post_comment'])); ?>
这是 post_comment 的字段信息:
这是提交的表单字段:
echo $this->Form->textarea('post_comment', array('label' => false, 'placeholder' => 'Ask a question or post a link', 'rows' => '3', 'class'=> 'u-full-width'));
编辑: 更奇怪的是 - 在应用程序的其他部分,撇号显示得很好。
autoLinkUrls
采用与 link()
相同的选项。所以它可以帮助。试试 -
<?php echo $this->Text->autoLinkUrls(h($item['Item']['post_comment']), array('escape' => false)); ?>
I don't want to have to do htmlspecialchars_decode() on every string in my app. Is there a CakePHP solution for this kind of issue?
是的,您需要了解,通过 CakePHP 核心帮助程序传递的所有内容在渲染之前都会使用 CakePHP 的 h()
函数进行内部清理,这是 htmlspecialchars()
的一个便利函数。 请注意,第三方助手可能不会那样做!所以请检查一下。
我认为几乎所有的辅助方法都有禁用转义的选项:
$this->Helper->method($foo, ['escape' => false]);
确保在禁用时不会意外允许输出恶意字符串。