Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL
Help

4. Template Directives > Defining Local Template Blocks - Pg. 147

The single section template can then be defined as: <p> <h2>[% title %]</h2> [% content %] </p> Like other block directives, it can be used in side-effect notation: [% INSERT legalese.txt WRAPPER big_bold_table %] It's also possible to specify multiple templates to a WRAPPER directive. The specifica- tion order indicates outermost to innermost wrapper templates. For example, given the following template block definitions: [% BLOCK bold %]<b>[% content %]</b>[% END %] [% BLOCK italic %]<i>[% content %]</i>[% END %] the directive: [% WRAPPER bold + italic %]Hello World[% END %] would generate the following output: <b><i>Hello World</i></b> Defining Local Template Blocks Sometimes, particularly in a project that involves a large number of small templates, it doesn't seem very efficient to create an external file for every template that you need. The BLOCK ... END construct can be used to avoid this. It allows you to define template component blocks that can be processed with the INCLUDE , PROCESS , and WRAPPER directives. [% BLOCK tabrow %] <tr><td>[% name %]<td><td>[% email %]</td></tr> [% END %] <table> [% PROCESS tabrow [% PROCESS tabrow </table> name='Fred' name='Alan' email='fred@nowhere.com' %] email='alan@nowhere.com' %] A BLOCK definition can be used before it is defined, as long as the definition resides in the same file. The block definition itself does not generate any output. [% PROCESS tmpblk %] [% BLOCK tmpblk %] This is OK [% END %] You can use an anonymous BLOCK to capture the output of a template fragment: [% julius = BLOCK %] And Caesar's spirit, ranging for revenge, With Ate by his side come hot from hell, Shall in these confines with a monarch's voice Defining Local Template Blocks | This is the Title of the Book, eMatter Edition Copyright © 2013 O'Reilly & Associates, Inc. All rights reserved. 147