Twig:无法覆盖宏中定义的块 [英] Twig: Cannot override a block that's defined in a macro

查看:26
本文介绍了Twig:无法覆盖宏中定义的块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从布局文件中导入一个宏,并在该宏中定义了一些树枝块.

I am importing a macro from a layout file, and inside that macro I define a few twig blocks.

当我在新的视图"文件中扩展我的基本"布局文件时,我无法覆盖我在宏中定义的块的内容.

When I extend my 'base' layout file in a new 'view' file, I cannot override the content of the blocks that I defined in the macro.

这是我的用例的一个非常简化的最小版本.

Here is a very simplified, minimal version of my use-case.

结构:

base.twig

{% import 'macros' as macros %}
<section>
    {{ macros.render_sections() }}

    {% block working %}
       content1-default
    {% endblock %}
</section>

ma​​cro.twig

{% macro render_sections() %}
    <section>
      {% block notworking %}
         content2-default
      {% endblock %}
    </section>
{% endmacro %}

view.twig

{% extends "base" %}

{% block working %}
   content1-override
{% endblock %}

{% block notworking %}
    content2-override
{% endblock %}

预期行为:我希望在我的 html 中看到content1-override content2-override".

Expected behavior: I expect to see "content1-override content2-override" in my html.

实际发生的事情:我看到'content1-override content2-default'

What actually happens: I see 'content1-override content2-default'

有没有办法将块作用域传递给宏?

Is there a way to pass the blocks scope to a macro?

我尝试在 base.twig 文件中定义宏,以排除导入功能,但这没有帮助.显然其他一切都有效,因为我可以看到确实被覆盖的块.

I have tried defining the macro inside the base.twig file, as to rule out the import function, but that didn't help. and obviously everything else works because I can see the block that does get overriden.

推荐答案

你应该了解 Twig 的内部结构:

You should learn about Twig's internals:

  • 所有 Twig 文件都转换为 PHP 类

  • all Twig files are converted to PHP classes

{% extends %} 相当于 PHP extends(用于继承).

{% extends %} is the equivalent for the litteral PHP extends (used for inheritance).

{% import %} 将上下文的属性分配给您正在导入的树枝文件"类型的对象

{% import %} assign a property of your context to an object of type "the twig file you're importing"

block 只不过是 php 方法,原生的 php 继承让你可以甜蜜地覆盖 Twig 块.

blocks are no more than php methods, and the native php inheritance let you overwrite Twig blocks sweetly.

因此,考虑到这一点,您转换后的树枝代码将如下所示:

So, taking this into account, your converted twig code will look like this:

class base {

   public function display() {
      $context['macros'] = new macros();
      $context['macros']->render_sections();
      echo '<section>';
      echo $this->blockWorking();
      echo '</section>';
   }

   public function blockWorking() {
     echo "content1-default";
   }

}

class macros {

  public function render_sections() {
     echo '<section>';
     echo $this->blockNotWorking();
     echo '</section>';
  }

  public function blockNotWorking() {
     echo "content2-defualt";
  }

}

class view extends base {

   public function blockWorking() {
     echo "content1-override";
   }

   public function blockNotWorking() {
     echo "content2-override";
   }

}

$view = new view();
$view->display();

这里可以清楚地看到,视图类的blockNotWorking()方法永远不会覆盖宏.

You can clearly see here that the blockNotWorking() method of the view class can never overwrite the macro.

这篇关于Twig:无法覆盖宏中定义的块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆