How to render branch macro in symfony 2 controller

I cannot figure out how to display the macro in the Symfony 2 controller. This is how I can display the branch template

$this
  ->get("twig")
  ->render("AcmeBundle:Product:table.html.twig", array(
    "product" => $product
  ))
;

      

So I'm looking for something similar but for rendering a branch macro. thanks for any suggestions!

+3


source to share


2 answers


A Twig macro is something inside a template. They run whenever you create a template that executes a macro.



+2


source


Just create another "wrapper" template that only contains this macro. Something like

macro.html.twig file

{% macro sample(item) %}
   {# some code here #}
{% endmacro sample #}

      

sample_macro_wrapper.html.twig



{% from 'macro.html.twig' import sample %}
{{ sample(item) }}

      

controller.php

public function someAction()
{
      // ...........
      $renderedMacro = $this->get('twig')
           ->render('sample_macro_wrapper.html.twig', ['item' => $item]);
}

      

0


source







All Articles