php - Symfony2 Why template rendered like string? -
i use service menu render , other menu features. when render menu, instead html menu receive menu string. why happens?
i have service.
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 )); } }
menu-template.html.twig
<div id="menu-"> {{ _self.rendermenu(links) }} </div> {% macro rendermenu(links) %} {% if links|length %} <ul> {% 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 %}
basic page template backend:
{% extends "::base.html.twig" %} {% block sidebar_left %} {{ menu.rendermenubyname('backend') }} {% endblock %}
all strings html escasped default, need use raw
prevent that:
{{ menu.rendermenubyname('backend')|raw }}
Comments
Post a Comment