dongluo8303 2016-09-07 04:40
浏览 25
已采纳

模板块继承Contao

I am new in contao development. 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:

<?php $this->block('name_of_the_block'); ?>

  // Block content

<?php $this->endblock(); ?>

If there are any contao developers here. 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.

  • 写回答

1条回答 默认 最新

  • dongwen3093 2016-09-07 22:04
    关注

    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 i.e. twig (to which we are moving in Contao 4 to).

    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:

     // ... 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:

     <?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. We can now override this block in another template again or override the label in another template extending 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.
    • 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题