If you use Cheetah, please support the project and register by clicking here:

8.8 #block ... #end block

The #block directive allows you to mark a section of your template that can be selectively reimplemented in a subclass. It is very useful for changing part of a template without having to copy-paste-and-edit the entire thing. The output from a template definition that uses blocks will be identical to the output from the same template with the #block ... #end block tags removed.

(Note: don't be confused by the generic word `block'' in this Guide, which means a section of code inside any #TAG ...#end TAG pair. Thus, an if-block, for-block, def-block, block-block etc. In this section we are talking only of block-blocks.)

To reimplement the block, use the #def directive. The magical effect is that it appears to go back and change the output text at the point the original block was defined rather than at the location of the reimplementation.

#block testBlock
Text in the contents 
area of the block directive
#if $testIt
#end if
#end block testBlock

You can repeat the block name in the #end block directive or not, as you wish.

#block directives can be nested to any depth.

#block outerBlock
Outer block contents 

#block innerBlock1
inner block1 contents 
#end block innerBlock1

#block innerBlock2
inner block2 contents 
#end block innerBlock2

#end block outerBlock

Note that the name of the block is optional for the #end block tag.

Technically, #block directive is equivalent to a #def directive followed immediately by a #placeholder for the same name. In fact, that's what Cheetah does. Which means you can use $theBlockName elsewhere in the template to output the block content again.

There is a one-line #block syntax analagous to the one-line #def.

The block must not require arguments because the implicit placeholder that's generated will call the block without arguments.

If you use Cheetah, please support the project and register by clicking here: