Free Trial

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

Share this Page URL

4. Template Directives > Perl Blocks - Pg. 172

generated output will be prefixed by a comment indicating the file, line, and original directive text. [% DEBUG on %] directive debugging is on (assuming DEBUG option is set to true) [% DEBUG off %] directive debugging is off The format parameter can be used to change the format of the debugging message: [% DEBUG format '<!-- $file line $line : [% $text %] -->' %] The DEBUG configuration option must be set to include DEBUG_DIRS for the DEBUG direc- tives to have any effect. If DEBUG_DIRS is not set, the parser will automatically ignore and remove any DEBUG directives. Perl Blocks The Template Toolkit directives that we have seen up to now together define a pre- sentation language that allows you to do just about anything you need to in order to control the display of your data. This is in keeping with the Template Toolkit philos- ophy of separating processing from presentation. However, there may be times when you want to go beyond what Template Toolkit offers you. Very occasionally you might need the power of a full programming lan- guage within your templates. When nothing else will do, the Template Toolkit also gives you the option of embedding Perl directly in your templates in PERL and RAWPERL directive blocks. Using PERL and RAWPERL blocks isn't something that is widely encouraged because it tends to make templates messy and hard to read. It also leads to a poor separation of concerns when you mix application code with presentation templates. However, the Template Toolkit doesn't enforce this separation, so you can embed Perl code inside your templates if you really want to. Because we don't encourage it, this feature is disabled by default. You will have to enable the EVAL_PERL configuration option to embed Perl code. PERL The PERL directive allows you to embed a block of Perl code in a template. It looks like this: [% PERL %] print "Hello world\n" [% END %] The EVAL_PERL configuration option must be enabled in order to use PERL blocks. If you try to use a PERL block when EVAL_PERL is disabled, a perl exception will be thrown with the message ` EVAL_PERL not set': 172 | Chapter 4: Template Directives This is the Title of the Book, eMatter Edition Copyright © 2013 O'Reilly & Associates, Inc. All rights reserved.