block

block — Defines a block that can be extended by a child template

Using function

<?php
block(string $name)

name: block name, create a new block with that same name in the child template to override it

As you see we redefined all the blocks here, however in the title block we used {$Tplix.parent}, which tells Tplix to insert the default content from the parent template in its place. The content block was overriden to display the images of the gallery, and page-title has also be overriden to display a completely different title.

Now if we render gallery_home we will get this:

Example #1

        {block "page-title"}Gallery home{/block}
        {block "content"}
          {foreach $images img}
            <img src="{$img.url}" alt="{$img.description}" />
          {/foreach}
        {/block}    
    

The above example will output:


    <div id="content">
      <img src="/example.jpg" alt="image" />
      <img src="/example2.jpg" alt="image" />
      <img src="/example3.jpg" alt="image" />
    </div>    
    
Defining new blocks in children templates is currently not supported.

Summary

To sum things up, here is a list of short hints and limitations that apply to template inheritance:

  • You currently can not close a block with the short-hand syntax {/}, you must explicitly do {/block}
  • The parent should define as much {block "name"}...{/block} parts as possible since it will be as flexible as the amount of blocks it has and this process happens entirely during template compilation so it adds nearly no overhead to the whole process
  • The children templates must start with {extends "parent"}
  • The children can not define any content besides what’s inside {block} tags they override, anything outside of {block} blocks will be removed
  • You can access the parent block content when overriding it through the {$Tplix.parent}
  • The parent can define multiple blocks with the same name, they will all be overriden at once if the child redefines it, and {$Tplix.parent} will vary according to each overriden block
  • The inheritance tree can be as big as you want (meaning you can extend a file that extends another one that extends another one and so on..), but be aware that all files have to be checked for modifications at runtime so the more inheritance the more overhead you add, but unless performance is really a concern for you it shouldn’t matter too much