我是组织发展的新手。我想要做的是在我的模板中扩展自定义块。所以,我的问题是:
是否可以在模板中创建自定义块?
在文档https://docs.contao.org/books/manual/3.5/en/04-managing-content/templates.html中有一个部分,其中写有模板继承,并声明我们可以继承自定义块,例如:
<?php $this->block('name_of_the_block'); ?>
// Block content
<?php $this->endblock(); ?>
如果这里有任何开发者。请帮帮我。真的很感激。感谢名单。如果你能列出其他重要的观点,那么它会有所帮助。谢谢。
答案 0 :(得分:4)
请记住,由于历史原因,Contao 3中的模板继承非常简约,不能与另一个引擎即twig(我们在Contao 4中移动到的那个)的灵活性进行比较。
回答你的问题: 您可以在模板中定义自己的块,然后可以在子模板中覆盖这些块。 事实上,每个街区都被创建了#34;在&#34; root&#34;它的名称模板然后被覆盖,看到这个在行动中引用表单元素模板,例如参见form_row.html5的代码:
// ... code omitted, refer to linked file.
<div class="<?= $this->prefix ?><?php if ($this->class) echo ' ' . $this->class; ?>">
<?php $this->block('label'); ?>
<?php $this->endblock(); ?>
<?php $this->block('field'); ?>
<?php $this->endblock(); ?>
</div>
// ... code omitted, refer to linked file.
这里引入了块,并在form_radio.html5中重写:
<?php $this->extend('form_row'); ?>
<?php $this->block('field'); ?>
// ... code omitted, refer to linked file.
<?php $this->endblock(); ?>
如您所见,正在覆盖块field
,而label
未被覆盖。
我们现在可以再次在另一个模板中覆盖此块,或覆盖另一个扩展form_radio.html5
的模板中的标签。
如上所述,有一些限制需要注意:
$this->extend()
的模板。这样做将以抛出异常结束。如果还有其他问题,请更新您的问题,因为猜测您想要知道的内容有点模糊。