我使用菜单渲染和其他菜单功能的服务。但是当我渲染菜单时,而不是html菜单我收到像字符串一样的菜单。为什么会这样?
我有服务。
services:
ad_backend.menu:
class: AdBackendBundle\Services\MenuService
arguments: [@doctrine.orm.entity_manager, @twig]
namespace AdBackendBundle\Services;
use Doctrine\ORM\EntityManager;
class MenuService
{
private $em;
private $twig;
public function __construct(EntityManager $em, \Twig_Environment $twig)
{
$this->em = $em;
$this->twig = $twig;
}
public function getMenuList()
{
return $this->em->getRepository('AdBackendBundle:AdMenu')->findAll();
}
public function renderMenuById($id)
{
$links = $this->em->getRepository('AdBackendBundle:AdMenu')->getLinksByMenuId($id);
return $this->twig->render('AdBackendBundle:Menu:menu-template.html.twig', array(
'links' => $links
));
}
public function renderMenuByName($name)
{
$links = $this->em->getRepository('AdBackendBundle:AdMenu')->getLinksByMenuName($name);
return $this->twig->render('AdBackendBundle:Menu:menu-template.html.twig', array(
'links' => $links
));
}
}
菜单template.html.twig
<div id="menu-">
{{ _self.renderMenu(links) }}
</div>
{% macro renderMenu(links) %}
{% if links|length %}
<ul>
{% for link in links %}
<li>
{% if link.route %}
<a href="{#{{ path(link.route) }}#}">{{ link.title }}</a>
{% elseif link.url %}
<a href="{{ link.url }}">{{ link.title }}</a>
{% endif %}
{% if link.children|length %}
{{ _self.renderMenu(link.children) }}
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
{% endmacro %}
后端的基本页面模板:
{% extends "::base.html.twig" %}
{% block sidebar_left %}
{{ menu.renderMenuByName('backend') }}
{% endblock %}
答案 0 :(得分:3)
默认情况下,所有字符串都是html,您需要使用raw
来阻止:
{{ menu.renderMenuByName('backend')|raw }}