gpt4 book ai didi

Symfony 2 & Twig,如何从扩展访问 block

转载 作者:行者123 更新时间:2023-12-01 15:31:59 25 4
gpt4 key购买 nike

我正在创建我的 Twig Extension 来扩展实际的“FormExtension”。这样做的原因是我需要在不覆盖当前函数的情况下创建新函数并使其在我的整个项目中可用。

因此,构建和扩展似乎是正确的方法。构建扩展不是问题,我的问题是如何从那里渲染 block ?

到目前为止,我的理解是,我需要创建一个 Twig_Environment,我必须在其中加载我的实际 Twig 模板(包含我的 block )。从那里我应该能够使用“$mytemplate->displayBlock()”呈现这些 block 。

示例代码:

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;

}

我通过查看 Symfony 基础 FormExtension.php 文件找到了这些信息。

我的问题是:

  1. displayBlock() 是如何工作的,我在哪里可以找到该函数的定义?
  2. 我上面描述的是正确的方法吗?
  3. 我应该如何继续访问新的 TWIG 模板以及基础 form_div_layout.html 模板?我能否以某种方式获取当前环境,而不必重新创建环境并在其中加载我的其他模板?

谢谢!

最佳答案

您是否尝试过使用 renderBlock 来代替?

您需要的第一个参数是 block 的名称,第二个参数应该是传递给 block 的值的关联数组。

因此,在呈现 block 的服务的情况下,您将拥有以下内容:

服务类:

<?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 %}

然后当你想显示它的时候,只需要调用刚刚创建的twig函数即可:

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

这绝不是一个完整的解决方案,但我已经使用了类似的东西并且证明它是有效的。

HTH

[编辑:2016 年 4 月 - 供引用:此解决方案适用于 Symfony 2.4 项目]

关于Symfony 2 & Twig,如何从扩展访问 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10584815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com