Permalink
Browse files

Added factory.t

git-svn-id: svn://svn.tt2.org/tt/Template2/trunk@147 d5a88997-0a34-4036-9ed2-92fb5d660d91
  • Loading branch information...
1 parent a4123f8 commit 351543be5ee6d14a9b1711c3a79c44b34c7686ed @abw committed Aug 4, 2001
Showing with 73 additions and 0 deletions.
  1. +73 −0 t/factory.t
View
@@ -0,0 +1,73 @@
+#============================================================= -*-perl-*-
+#
+# t/factory.t
+#
+# Test use of a modified directive factory, based on something that
+# pudge suggested on #perl.
+#
+# Written by Andy Wardley <abw@kfs.org>
+#
+# Copyright (C) 1996-2001 Andy Wardley. All Rights Reserved.
+#
+# This is free software; you can redistribute it and/or modify it
+# under the same terms as Perl itself.
+#
+# $Id$
+#
+#========================================================================
+
+use strict;
+use lib qw( ./lib ../lib );
+use Template::Test;
+$^W = 1;
+
+# uncomment these lines to see how generate Perl code
+# for constant.* is expanded at parse time
+#Template::Parser::DEBUG = 1;
+#Template::Directive::PRETTY = 1;
+
+package My::Directive;
+use base qw( Template::Directive );
+
+my $constants = {
+ pi => 3.14,
+ e => 2.718,
+};
+
+sub ident {
+ my ($class, $ident) = @_;
+
+ # note single quoting of 'constant'
+ if (ref $ident eq 'ARRAY' && $ident->[0] eq "'constant'") {
+ my $key = $ident->[2];
+ $key =~ s/'//g;
+ return $constants->{ $key } || '';
+ }
+ return $class->SUPER::ident($ident);
+}
+
+package main;
+
+my $cfg = {
+ FACTORY => 'My::Directive',
+};
+
+my $vars = {
+ foo => {
+ bar => 'Place to purchase drinks',
+ baz => 'Short form of "Basil"',
+ },
+};
+
+test_expect(\*DATA, $cfg, $vars);
+
+__DATA__
+-- test --
+[% foo.bar %]
+-- expect --
+Place to purchase drinks
+
+-- test --
+[% constant.pi %]
+-- expect --
+3.14

0 comments on commit 351543b

Please sign in to comment.