Free Trial

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


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 4. Template Directives > Template Metadata - Pg. 161

%] ELSE; words.push(word); END; END; words.join(' '); END This macro splits the text passed as an argument into words, attempts to explain them, and then joins them back up into a single piece of text: [% translate( "Hey you sass that hoopy Ford Prefect? There's a frood who really knows where his towel is." ) %] This is the output generated by the previous template fragment: Hey you sass (know, be aware of, meet, have sex with) that hoopy (really together guy) Ford Prefect? There's a frood (really, amazingly together guy) who really knows where his towel is. A MACRO can also be defined as a PERL block, but will require the EVAL_PERL option to be set: [% MACRO triple(n) PERL %] my $n = $stash->get('n'); print $n * 3; [% END -%] The PERL and RAWPERL directives are covered at the end of this chapter. Template Metadata The Template Toolkit compiles a template into a Perl object (an instance of the class Template::Document ). This object contains Perl code that reproduces the required be- havior of the template. You can access the data in this object via the template variable. The Template::Document has access to various items of metadata about the template that you can access via template . This always includes the name of the template and the last modification time, so it is always possible to include things such as this in your template: [% USE date(format => '%Y-%m-%d %H:%M:%S') %] [% template.name %] Last modified: [% date.format(template.modtime) %] Further metadata items can be added using the META directive, discussed next. These new items will also be available through the template variable. [% META moon_phase = 'first quarter' -%] Phase of moon: [% template.moon_phase %] Template Metadata | 161