1

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.

DpEN
  • 4,216
  • 3
  • 17
  • 26

1 Answers1

4

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.

xtra
  • 748
  • 9
  • 13
  • Thank you for the explanation. I think i get some portion of the block but still to learn. So, your explanation means that we cannot create our own blocks but only extend what already exists from parent templates right ? So, when extending the form_row whatever we post into block('field'); ?> will be shown overiding the previous elements ? – DpEN Sep 08 '16 at 10:53
  • Correct, you can either override blocks (put new content or wrap the content from the parent) when extending OR define available blocks when NOT extending, not both in the same template. – xtra Sep 08 '16 at 17:11