I'm using the KNP Menu Bundle within my Symfony2 project and create my Menus as a service.
I have the problem that the Route Labels quotes and other special chars won't displayed correctly.
As an example:
Test Text & Stuff
will be displayed as Test Text & Stuff
and I can't figure out how do deal with it.
I'm creating the route as following:
$menu->addChild('seller', array(
'route' => 'routename',
'routeParameters' => $array,
'label' => $sellername
))->setLinkAttribute('class', 'dark-color active-hover');
I tried to this commands to get rid of it:
But neither of them worked. It wouldn't be a big deal if the browser would translate them corectly, but the browser isn't doing it because of this:
Test Text & Stuff
There's plenty of whitespace before and after my text and I can't figure out where it's comming from. I trimed the $sellername
and I also added the trim commands from twig into the knp_menu.html.twig
.
Any suggestions how I can deal with this situation?
Edit:
What I figured out now is that if I manually remove the whitespaces from the text the text will be displayed correctly. I tried to trim the whitespaces with javascript, but I had no success with it by now.
Edit:
Here's the knp_menu.html.twig template
{% 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 %}
答案 0 :(得分:3)
默认情况下,在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 }}
详细了解有关raw filter和escaper extension
的文档