Commit | Line | Data |
e374d8da |
1 | |
2 | package Trexy::Template; |
3 | |
4 | ############################################################################### |
5 | # Nigel Hamilton |
6 | # |
7 | # Copyright Nigel Hamilton 2005 |
8 | # All Rights Reserved |
9 | # |
10 | # Author: Nigel Hamilton |
11 | # Filename: Trexy::Template.pm |
12 | # Description: Do simple, fast, "no code in the template" processing |
13 | # |
14 | # Based on Uri Guttman's sublime |
15 | # TinyTemplate module - released |
16 | # at YAPC::NA, 2006. |
17 | # |
18 | # This template module features: |
19 | # |
20 | # * implicit loops |
21 | # * template includes |
22 | # * template caching |
23 | # * no coding in the template |
24 | # |
25 | # Date Change |
26 | # ----------------------------------------------------------------------------- |
27 | # 24/09/2006 Took Uri's module and extended it slightly |
28 | # |
29 | ############################################################################### |
30 | |
31 | use strict; |
32 | use warnings ; |
33 | |
34 | use base qw(Template::Simple); |
35 | |
36 | # escape Regex meta characters |
37 | my $start_delimiter = qr/\[\-/; |
38 | my $end_delimiter = qr/\-\]/; |
39 | |
40 | |
41 | ############################################################################### |
42 | # |
43 | # new - construct a template |
44 | # |
45 | ############################################################################### |
46 | |
47 | sub new { |
48 | |
49 | my ($class) = @_; |
50 | |
51 | return $class->SUPER::new( pre_delim => $start_delimiter, |
52 | post_delim => $end_delimiter ); |
53 | |
54 | } |
55 | |
56 | |
57 | ############################################################################### |
58 | # |
59 | # render - fully render the template into a string |
60 | # |
61 | ############################################################################### |
62 | |
63 | sub render { |
64 | |
65 | my ($this, $string, $tokens) = @_; |
66 | |
67 | # return a string from render |
68 | return ${ $this->SUPER::render($string, $tokens) }; |
69 | |
70 | } |
71 | |
72 | |
73 | ############################################################################### |
74 | # |
75 | # render_as_ref - render as a string reference |
76 | # |
77 | ############################################################################### |
78 | |
79 | sub render_as_ref { |
80 | |
81 | my ($this, $string, $tokens) = @_; |
82 | |
83 | # return a string from render |
84 | return $this->SUPER::render($string, $tokens); |
85 | |
86 | } |
87 | |
88 | |
89 | ############################################################################### |
90 | # |
91 | # get_tokens - grab the tokens values out of a template |
92 | # |
93 | ############################################################################### |
94 | |
95 | sub get_tokens { |
96 | |
97 | my ($string) = @_; |
98 | |
99 | my @found_tokens = $string =~ m/$start_delimiter(\w+)$end_delimiter/g; |
100 | |
101 | my %unique_tokens = map { $_ => 1 } @found_tokens; |
102 | |
103 | return keys %unique_tokens; |
104 | |
105 | } |
106 | |
107 | |
108 | 1; |