Php 带有HTML特殊字符的KNP菜单包
我在Symfony2项目中使用KNP菜单包,并将菜单作为服务创建 我的问题是路线标签、引号和其他特殊字符不能正确显示 例如:Php 带有HTML特殊字符的KNP菜单包,php,html,symfony,knpmenubundle,Php,Html,Symfony,Knpmenubundle,我在Symfony2项目中使用KNP菜单包,并将菜单作为服务创建 我的问题是路线标签、引号和其他特殊字符不能正确显示 例如: Test Text&Stuff将显示为Test Text&;我不知道该怎么处理 我正在按如下方式创建路线: $menu->addChild('seller', array( 'route' => 'routename', 'routeParameters' => $array, 'label' => $sellerna
Test Text&Stuff
将显示为Test Text&;我不知道该怎么处理
我正在按如下方式创建路线:
$menu->addChild('seller', array(
'route' => 'routename',
'routeParameters' => $array,
'label' => $sellername
))->setLinkAttribute('class', 'dark-color active-hover');
我试着用这个命令来摆脱它:
html_实体_解码()
htmlspecialchars_解码()
htmlspecialchars()
htmlentities()
但他们两人都没有成功。如果浏览器能够正确地翻译它们,那也没什么大不了的,但浏览器之所以没有这样做,是因为:
Test Text & Stuff
我的文本前后都有大量的空白,我不知道它是从哪里来的。我修剪了$sellername
,还将trim命令从twig添加到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 %}
{%extends'knp_menu.html.twig%}
{%block item%}
{%import“knp_menu.html.twig”作为宏%}
{%if item.displated%}
{%-set attributes=item.attributes%}
{%-set is_dropdown=attributes.dropdown | default(false)%}
{%-set icon=attributes.icon |默认值(false)%}
{%-set span=attributes.span |默认值(false)%}
{%-set spanContent=attributes.spanContent |默认值(false)%}
{%-set notification=attributes.notification |默认值(false)%}
{%-set divider_prepend=attributes.divider_prepend|default(false)%}
{%-set divider_append=attributes.divider_append|default(false)%}
{#取消设置引导特定属性#}
{%-set attributes=attributes | merge({'dropdown':null,'icon':null,'span':null,'spanContent':null,'notification':null,'divider_prepend':null,'divider_append':null})%}
{%-if除法器_prepend%}
{{block('dividerelation')}
{%-endif%}
{#构建项的类#}
{%-set classes=item.attribute('class')不是空的?[item.attribute('class')]:[]%}
{%-if matcher.isCurrent(item)%}
{%-集合类=类|合并([options.currentClass])%}
{%-elseif matcher.isAncestor(项,选项.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%}
{#建立儿童班级}
{%-set-childrenClasses=item.childrenAttribute('class')不是空的?[item.childrenAttribute('class')]:[]%}
{%-set childrenClasses=childrenClasses | merge(['menu_level.'~item.level])%}
{#为下拉菜单添加类#}
{%-如果是_下拉列表%}
{%-如果item.level>1%}
{%-set classes=classes | merge(['dropdown-submenu'])%}
{%-else%}
{%-set classes=classes | merge(['dropdown]])%}
{%-endif%}
{%-set childrenclass=childrenclass | merge(['dropdown-menu'])%}
{%-endif%}
{#把课程放在一起}
{%-如果类不是空的%}
{%-set attributes=attributes | merge({'class':类| join('')})%}
{%-endif%}
{%-set listtributes=item.childrenAttributes | merge({'class':childrenclass | join('')})%}
{#显示项目#}
{%-如果是_下拉列表%}
{{-block('dropdownElement')-}
{%-elseif item.uri不为空且(不是matcher.isCurrent(item)或options.currentAsLink)%}
{{-block('linkElement')-}
{%-else%}
{{-block('spanElement')-}
{%-endif%}
{呈现子对象列表}
{{-block('list')-}
{%-if除法器_append%}
{{block('dividerelation')}
{%-endif%}
{%endif%}
{%endblock%}
{%block linkElement%}
{%endblock%}
{%block dividerelation%}
{%if item.level==1%}
{%else%}
{%endif%}
{%endblock%}
{%block dropdownElement%}
{%-set-classes=item.linkAttribute('class')不是空的?[item.linkAttribute('class')]:[]%}
{%-set classes=classes | merge(['dropdown-toggle'])%}
{%-set attributes=item.linkAttributes%}
{%-set attributes=attributes | merge({'class':类| join('')})%}
{%-set attributes=attributes | merge({'data-toggle':'dropdown'})%}
{%endblock%}
{%block label%}{{item.label | trim | trans}{%endblock%}
raw做什么?
默认情况下,当在Twig中显示某些内容时(使用{{}
),Twig将对其进行转义以确保其安全。我们不希望任何邪恶的HTML出现在我们的页面中
有时我们想要显示的东西已经是HTML了,所以我们不想再逃避它。这就是raw
过滤器的作用!它告诉twig,它刚刚“过滤”的东西已经转义,我们希望以它的原始形式使用它(例如,不要再次转义)
如何使用生料
block('label')
将渲染名为“label”的块。此呈现将在html安全的情况下完成,这意味着它将被转义
通过在另一个模板中显示它(通过执行{{block('label')}}
),它将