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 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了