[英]KNP Menu Bundle with HTML Special Chars
我正在Symfony2项目中使用KNP菜单包并创建我的菜单作为服务。
我有一个问题,路由标签引号和其他特殊字符将无法正确显示。
举个例子:
Test Text & Stuff
将显示为Test Text & Stuff
Test Text & Stuff
和我无法弄清楚如何处理它。
我正在创建如下路线:
$menu->addChild('seller', array(
'route' => 'routename',
'routeParameters' => $array,
'label' => $sellername
))->setLinkAttribute('class', 'dark-color active-hover');
我尝试了这个命令来摆脱它:
但他们都没有奏效。 如果浏览器能够对其进行核心翻译,那就没什么大不了的了,但浏览器并没有这样做:
Test Text & Stuff
在我的文本之前和之后有很多空白,我无法弄清楚它来自何处。 我对$sellername
sellername进行$sellername
并且还将$sellername
的trim命令添加到了knp_menu.html.twig
。
有什么建议我可以处理这种情况吗?
编辑:
我现在想到的是,如果我手动删除文本中的空格,文本将正确显示。 我试图用javascript修剪空白,但是现在我没有成功。
编辑:
这是knp_menu.html.twig模板
{% extends 'knp_menu.html.twig' %}
{% block item %}
{% import "knp_menu.html.twig" as macros %}
{% if item.displayed %}
{%- set attributes = item.attributes %}
{%- set is_dropdown = attributes.dropdown|default(false) %}
{%- set icon = attributes.icon|default(false) %}
{%- set span = attributes.span|default(false) %}
{%- set spanContent = attributes.spanContent|default(false) %}
{%- set notification = attributes.notification|default(false) %}
{%- set divider_prepend = attributes.divider_prepend|default(false) %}
{%- set divider_append = attributes.divider_append|default(false) %}
{# unset bootstrap specific attributes #}
{%- set attributes = attributes|merge({'dropdown': null, 'icon': null, 'span': null, 'spanContent': null, 'notification': null, 'divider_prepend': null, 'divider_append': null }) %}
{%- if divider_prepend %}
{{ block('dividerElement') }}
{%- endif %}
{# building the class of the item #}
{%- set classes = item.attribute('class') is not empty ? [item.attribute('class')] : [] %}
{%- if matcher.isCurrent(item) %}
{%- set classes = classes|merge([options.currentClass]) %}
{%- elseif matcher.isAncestor(item, options.depth) %}
{%- set classes = classes|merge([options.ancestorClass]) %}
{%- endif %}
{%- if item.actsLikeFirst %}
{%- set classes = classes|merge([options.firstClass]) %}
{%- endif %}
{%- if item.actsLikeLast %}
{%- set classes = classes|merge([options.lastClass]) %}
{%- endif %}
{# building the class of the children #}
{%- set childrenClasses = item.childrenAttribute('class') is not empty ? [item.childrenAttribute('class')] : [] %}
{%- set childrenClasses = childrenClasses|merge(['menu_level_' ~ item.level]) %}
{# adding classes for dropdown #}
{%- if is_dropdown %}
{%- if item.level > 1 %}
{%- set classes = classes|merge(['dropdown-submenu']) %}
{%- else %}
{%- set classes = classes|merge(['dropdown']) %}
{%- endif %}
{%- set childrenClasses = childrenClasses|merge(['dropdown-menu']) %}
{%- endif %}
{# putting classes together #}
{%- if classes is not empty %}
{%- set attributes = attributes|merge({'class': classes|join(' ')}) %}
{%- endif %}
{%- set listAttributes = item.childrenAttributes|merge({'class': childrenClasses|join(' ') }) %}
{# displaying the item #}
<li{{ macros.attributes(attributes) }}>
{%- if is_dropdown %}
{{- block('dropdownElement') -}}
{%- elseif item.uri is not empty and (not matcher.isCurrent(item) or options.currentAsLink) %}
{{- block('linkElement') -}}
{%- else %}
{{- block('spanElement') -}}
{%- endif %}
{# render the list of children#}
{{- block('list') -}}
</li>
{%- if divider_append %}
{{ block('dividerElement') }}
{%- endif %}
{% endif %}
{% endblock %}
{% block linkElement %}
<a href="{{ item.uri }}"{{ knp_menu.attributes(item.linkAttributes) }}>
{% if item.attribute('icon') is not empty %}
<i class="{{ item.attribute('icon') }}"></i>
{% endif %}
{{ block('label')|trim }}
{% if item.attribute('notification') is not empty %}
<span class="bagde"><icon class=" {{ item.attribute('notification') }}"></icon></span>
{% endif %}
{% if item.attribute('span') is not empty %}
<span class="{{ item.attribute('span') }}">{% if item.attribute('spanContent') is not empty %}{{ item.attribute('spanContent')}}{% endif %}</span>
{% endif %}
</a>
{% endblock %}
{% block dividerElement %}
{% if item.level == 1 %}
<li class="sidebar-divider"></li>
{% else %}
<li class="divider"></li>
{% endif %}
{% endblock %}
{% block dropdownElement %}
{%- set classes = item.linkAttribute('class') is not empty ? [item.linkAttribute('class')] : [] %}
{%- set classes = classes|merge(['dropdown-toggle']) %}
{%- set attributes = item.linkAttributes %}
{%- set attributes = attributes|merge({'class': classes|join(' ')}) %}
{%- set attributes = attributes|merge({'data-toggle': 'dropdown'}) %}
<a href="#"{{ macros.attributes(attributes) }}>
{% if item.attribute('icon') is not empty %}
<i class="{{ item.attribute('icon') }}"></i>
{% endif %}
{{ block('label')|trim }}
{% if item.level <= 1 %} <b class="caret"></b>{% endif %}</a>
{% endblock %}
{% block label %}{{ item.label|trim|trans }}{% endblock %}
默认情况下,当在Twig中显示某些内容时(使用{{ }}
),Twig会将其转义以确保它是安全的。 我们不希望任何邪恶的HTML在我们的页面中结束。
有时我们想要显示的东西已经是HTML,所以我们不想再次逃避它。 这就是raw
过滤器的用武之地! 它告诉twig它刚刚“过滤”的东西已经被转义,我们希望以它的原始形式使用它(例如,不再逃避它)。
block('label')
将呈现一个名为“label”的块。 这个渲染将完成html-safe,这意味着它被转义。
通过在另一个模板中显示它(通过执行{{ block('label') }}
),它将再次转义。 如果你不想这样,你应该使用raw
过滤器: {{ block('label')|raw }}
raw
过滤器必须最后使用,否则无效。 因此,如果要修剪渲染,请先进行渲染: {{ block('label')|trim|raw }}
PS:即使做更精细的事情,你仍然必须使用raw
最后和整体。 例如: {{ ('<span>' ~ block('label')|trim ~ '</span>')|raw }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.