[Templates-cvs] cvs commit: TT3/examples README directive.pl
cvs@template-toolkit.org
cvs@template-toolkit.org
Sat, 11 Dec 2004 13:57:37 +0000
cvs 04/12/11 13:57:37
Added: examples README directive.pl
Log:
* added new examples and test templates
Revision Changes Path
1.1 TT3/examples/README
Index: README
===================================================================
This directory contains some examples of TT3 in action.
tt3.pl # process a template via the Template module
compiler.pl # shows the Perl code generated by the compiler
directive.pl # writing a custom directive
htmlt.pl # proof-of-concept for HTML::Template-like language
scanner.pl # creating a custom template scanner
1.1 TT3/examples/directive.pl
Index: directive.pl
===================================================================
#!/usr/bin/perl -w # -*- perl -*-
#
# This example shows how a custom directive can be created. We
# define a My::Directive::Hello directive to parse an imaginary
# HELLO directive, and also a custom generator to generate Perl
# code for it. Then we plug it all in and compile a template.
#
use strict;
use warnings;
use lib qw( ../lib ./lib );
use Template::TT3::Compiler;
use Template::Directive;
use Template::Generator::Perl;
# run with '-d' option to enable debugging
my $DEBUG =
$Template::Tagset::DEBUG =
$Template::Tag::Directive::DEBUG =
$Template::Factory::Modules::DEBUG =
$Template::Factory::DEBUG =
grep(/^--?d(ebug)?/, @ARGV);
#------------------------------------------------------------------------
# First we define an object to parse the directive into a structured
# expression. The Template::Parser (in $match->{ parser }) does most
# of the hard work.
#------------------------------------------------------------------------
package My::Directive::Hello;
use base 'Template::Directive';
our $KEYWORD = 'HELLO';
sub parse {
my ($self, $textref, $handler, $match) = @_;
print STDERR "parsing HELLO directive at line $match->{ line }\n";
# just like the INCLUDE directive, our HELLO directive
# expects a list of one or more paths (separated by '+'),
# and then an optional list of parameters
my $parser = $match->{ parser };
my $paths = $parser->parse_paths($textref)
|| return $self->error('missing paths in ',
$self->keyword($match),
' directive');
my $params = $parser->parse_params($textref);
return $handler->expr( hello => $paths, $params );
}
#------------------------------------------------------------------------
# next we subclass the Perl generator to generate some Perl code for
# the directive. We also redefine the template_modules() method to
# add our Template::Resource::Hello object to the list of modules loaded
# by this template. This module (not shown) defines the hello() method
# in the Template::Context that our generated code is going to call, and
# implements whatever particular functionally is required of HELLO.
#------------------------------------------------------------------------
package My::Generator::Perl;
use base 'Template::Generator::Perl';
sub generate_hello {
my ($self, $paths, $params) = @_;
$paths = $self->generate_paths($paths);
$params = $self->generate_params($params);
return $self->output("\$context->hello($paths, $params)");
}
sub template_modules {
my $self = shift;
my $modules = $self->SUPER::template_modules();
return <<EOF;
$modules
use My::Resource::Hello;
EOF
}
#------------------------------------------------------------------------
# now we go ahead and use it.
#------------------------------------------------------------------------
my $compiler = Template::TT3::Compiler->new({
directives => {
HELLO => 'My::Directive::Hello',
hello => 'My::Directive::Hello',
},
loaded => 'HELLO hello',
generator => My::Generator::Perl->new( pretty => 1 ),
}) || die Template::TT3::Compiler->error();
# read input from __DATA__ section
local $/ = undef;
my $input = <DATA>;
# compile input template into Perl code
my $output = $compiler->compile($input, name => 'compiler example')
|| die $compiler->error();
# Look ma! A compiled template!
print "-- input --\n",
$input, "\n",
"-- output --\n",
$output, "\n";
__DATA__
[% HELLO world %]
[% HELLO world + 'his wife'
title = 'Global Greeting'
%]
Also works in side-effect:
[% HELLO world IF happy %]