如何使用奏鸣曲块Bundle渲染块

时间:2016-02-27 13:33:38

标签: php symfony twig sonata-admin twig-extension

我创建了一个块,并在块中添加了一些内容。我想在页面myblock.html.twig中呈现我的块的内容

这是页面:myblock.html.twig

    {% extends sonata_block.templates.block_base %}

{% block block %}
    {{ sonata_block_render({'type':'sonata.block.service.myblock'}) }}
{% endblock %}

刷新页面时出现以下错误:

  

此网页不可用

     

ERR_CONNECTION_RESET

我的服务器仍在运行,我可以访问该网站的其他页面。

MyBlockService.php

    <?php

namespace FLY\BookingsBundle\Block;
use Symfony\Component\HttpFoundation\Response;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\CoreBundle\Validator\ErrorElement;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\BaseBlockService;
use Doctrine\ORM\EntityManager;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class MyBlockService extends BaseBlockService
{
    private $em;

    /**
     * {@inheritdoc}
     */


    public function execute(BlockContextInterface $block, Response $response = null)
    {
        // merge settings
        $settings = array_merge($this->getDefaultSettings(), $block->getSettings());
        return $this->renderResponse('FLYBookingsBundle:Default:myblock.html.twig', array(
            'block'     => $block,
            'settings'  => $settings
        ), $response);
    }

    /**
     * {@inheritdoc}
     */
    public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
    {
        // TODO: Implement validateBlock() method.
    }

    /**
     * {@inheritdoc}
     */
    public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
    {
        $formMapper->add('settings', 'sonata_type_immutable_array', array(
            'keys' => array(
                array('content', 'textarea', array()),
            )
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'Text (core)';
    }

    /**
     * {@inheritdoc}
     */
    public function getDefaultSettings()
    {
        return array();
    }


    /**
     * {@inheritdoc}
     */
    public function configureSettings(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'content'    => 'Insert your custom content here',
        ));
    }


    public function __construct($name, $templating, EntityManager $entityManager)
    {
        parent::__construct($name, $templating);
        $this->em = $entityManager;
    }}

service.yml

sonata.block.service.myblock:
          class: FLY\BookingsBundle\Block\MyBlockService
          arguments: [ "sonata.block.service.myblock", @templating, @doctrine.orm.entity_manager ]
          tags:
            - { name: sonata.block }

0 个答案:

没有答案