Séb*_*ien 15 html strip special-characters symfony twig
我正在使用twig渲染视图,我正在使用striptags过滤器来删除html标记.但是,html特殊字符现在呈现为文本,因为整个元素被""包围.我怎样才能剥离特殊字符或渲染它们,同时仍然使用striptags函数?
示例:
{{ organization.content|striptags(" >")|truncate(200, '...') }}
Run Code Online (Sandbox Code Playgroud)
要么
{{ organization.content|striptags|truncate(200, '...') }}
Run Code Online (Sandbox Code Playgroud)
输出:
"QUI SOMMES NOUS ? > NOS LOCAUXNOS LOCAUXDepuis 1995, Ce lieu chargé d’histoire et de tradition s’inscrit dans les valeurs"
Run Code Online (Sandbox Code Playgroud)
Ely*_*ass 35
如果它可以帮助别人,这是我的解决方案
{{ organization.content|striptags|convert_encoding('UTF-8', 'HTML-ENTITIES') }}
Run Code Online (Sandbox Code Playgroud)
您还可以添加修剪过滤器以删除前后的空格.然后,您截断或切片您的organization.content
编辑2017年11月
如果你想保持"\n"断行与截断相结合,你可以这样做
{{ organization.content|striptags|truncate(140, true, '...')|raw|nl2br }}
我正在尝试其中一些答案:
{{ organization.content|striptags|truncate(200, true) }}
{{ organization.content|raw|striptags|truncate(200, true) }}
{{ organization.content|striptags|raw|truncate(200, true) }}
etc.
Run Code Online (Sandbox Code Playgroud)
最终形式中仍然有奇怪的角色。对我有帮助的是,将raw过滤器放在所有操作的末尾,即:
{{ organization.content|striptags|truncate(200, '...')|raw }}
Run Code Online (Sandbox Code Playgroud)
我尝试了投票最高的推荐。它适用于某些符号,但不适用于其他符号。
raw 过滤器似乎可以正常处理所有特殊字符。
像这样
{{ organization.content|striptags|raw }}
我有类似的问题,这对我有用:
{{ variable |convert_encoding('UTF-8', 'HTML-ENTITIES') | raw }}
Run Code Online (Sandbox Code Playgroud)
Arf,我终于找到了:
我正在使用仅应用 php 函数的自定义树枝过滤器:
<span>{{ organization.shortDescription ?: php('html_entity_decode',organization.content|striptags|truncate(200, '...')) }}</span>
Run Code Online (Sandbox Code Playgroud)
现在它正确呈现
我的 php 扩展:
<?php
namespace AppBundle\Extension;
class phpExtension extends \Twig_Extension
{
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('php', array($this, 'getPhp')),
);
}
public function getPhp($function, $variable)
{
return $function($variable);
}
public function getName()
{
return 'php_extension';
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25502 次 |
| 最近记录: |