Symfony 2 & Twig how to access a block from an extension

I am creating a Twig extension to extend the actual "FormExtension". The reason is that I need to create new functions without overwriting the current ones, and make this available throughout my project.

Thus, construction and expansion seemed to be the right way. Building an extension is not a problem, my problem is how to draw a block from there?

What I have understood so far is that I need to create Twig_Environment where I need to load my actual branch template (containing my blocks). From there, I should be able to display these blocks using "$ mytemplate-> displayBlock ()".

Code example:

public function renderWidgetinline (FormView $ view, array $ variables = array ()) {

  $loader = new \Twig_Loader_Filesystem(__DIR__.'/../Resources/views/Form');
  $twig = new \Twig_Environment($loader);
  $this->template = $twig->loadTemplate("form_layout.html.twig");

  ob_start();
  $this->template->displayBlock(???WHAT-PARAMS???);
  $html = ob_get_clean();

  return $html;

}

, FormExtension.php Symfony.

:

  • displayBlock(), ?
  • , , ?
  • TWIG form_div_layout.html? - , , ?

!

+3
1

renderBlock ?

, , - , , .

, , , :

:

<?php

namespace Acme\BlockBundle\Blocks;

use Doctrine\Common\Persistence\ObjectManager;

Class Block {

    private $om;
    private $environment;
    private $template;

    public function __construct( ObjectManager $om, Twig $environment )
    {
        $this->om = $om;
        $this->environment = $environment;
    }

    public function render( $template, $data )
    {
        $this->template = $this->environment->loadTemplate( $template );

        // maybe query the DB via doctrine, that is why I have included $om
        // in the service arguments
        // example:

        $entities = $om->getRepository( 'AcmePizzaBundle:Pizza' )->getMeatyOnes()

        return $this->template->renderBlock( 'acme_block', array(
            'data' => $entities,
        ));
    }
}

Twig

<?php

namespace Acme\BlockBundle\Twig\Extension;

use Twig_Extension;
use Twig_Function_Method;

class BlockExtension extends Twig_Extension
{
    protected $container;

    public function __construct( $container )
    {
        $this->container = $container;
    }

    public function getName()
    {
        return 'block_extension';
    }

    public function getFunctions()
    {
        return array(
            'render_block' => new Twig_Function_Method( $this, 'renderBlock', array(
                'is_safe' => array( 'html' ),
            )),
        );
    }

    public function renderBlock( $template, $data )
    {
        return $this->container->get( 'acme.block' )->render( $template, $data );
    }
}

.yml

parameters:
    acme.blocks.block.class:           Acme\BlocksBundle\Blocks\Block
    acme.twig.block_extension.class:   Acme\BlocksBundle\Twig\Extension\BlockExtension

services:
    acme.blocks.block:
        class:  '%acme.blocks.block.class%'
        arguments:
            - '@doctrine.orm.entity_manager'
            - '@twig'

acme.twig.block_extension:
    class: %acme.twig.block_extension.class%
    arguments:
        - '@service_container'
    tags:
        - { name: twig.extension }

:

{% block acme_block %}
    {% spaceless %}
        {# do something with your data here #}
    {% endspaceless %}
{% endblock acme_block %}

, , , :

{{ render_block( '::block_template.html.twig', someDataOneThePage ) }}

, - , .

[edit: April 2016 - : Symfony 2.4]

+2

All Articles