简体   繁体   English

模板块继承Contao

[英]Template Block Inheritence Contao

I am new in contao development. 我是contao开发的新手。 What i am trying to do is extend custom blocks in my templates. 我想做的是在模板中扩展自定义块。 So, my questions is: 因此,我的问题是:

Is it possible to create custom blocks in templates? 是否可以在模板中创建自定义块?

In the Documentation https://docs.contao.org/books/manual/3.5/en/04-managing-content/templates.html there is a section where it is written template inheritence and stated that we can inherit custom blocks for example: 在文档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(); ?>

If there are any contao developers here. 如果这里有任何contao开发人员。 Please help me out. 请帮帮我。 Would really appreciate it. 非常感谢。 Thanx. 谢谢 If you can list out other important points also then it would be helpful. 如果您还可以列出其他要点,那将很有帮助。 Thank you. 谢谢。

Please keep in mind that the template inheritance in Contao 3 is pretty minimalistic due to historic reasons and not to be compared with the flexibility another engine ie twig (to which we are moving in Contao 4 to). 请记住,由于历史原因,Contao 3中的模板继承非常简单,因此不能与其他引擎(即Twig)(我们将在Contao 4中迁移到)的灵活性进行比较。

To answer your question: You can define own blocks in your templates which can then be overridden in a child template. 要回答您的问题:您可以在模板中定义自己的块,然后可以在子模板中将其覆盖。 In fact, every block is "created" in the "root" template of it's name and then overridden, to see this in action refer to the form element templates for example see the code of form_row.html5 : 实际上,每个块都是在其名称的“根”模板中“创建”的,然后被覆盖,要在操作中看到此信息,请参考表单元素模板,例如,参见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.

The blocks are introduced here and overridden in form_radio.html5 : 此处介绍了这些块,并在form_radio.html5对其进行了覆盖:

 <?php $this->extend('form_row'); ?>

 <?php $this->block('field'); ?>
 // ... code omitted, refer to linked file.
 <?php $this->endblock(); ?>

As you can see, the block field is being overridden and label is not. 如您所见,block field被覆盖, label未被覆盖。 We can now override this block in another template again or override the label in another template extending form_radio.html5 . 现在,我们可以再次覆盖另一个模板中的该块,或者覆盖另一个扩展了form_radio.html5模板中的标签。

As stated above, there are some limitations to be aware of: 如上所述,要注意一些限制:

  • You are not allowed to introduce new blocks in a child template (one that uses $this->extend() . Doing so will end in an Exception being thrown. 不允许在子模板(使用$this->extend()的子模板)中引入新的块,否则将引发Exception。
  • You can not introduce code outside of a block in a child template. 您不能在子模板的块外引入代码。

If there should be remaining questions, please update your question as it is a bit vague to guess what exactly you want to know. 如果还有其他问题,请更新您的问题,因为猜测您确切想知道的内容有点含糊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM