]> mj.ucw.cz Git - temple.git/blob - test
temple -e
[temple.git] / test
1 @# A test program for Temple
2 @# starts with a couple of comments
3
4 @# The language is very simple -- everything except the "@" character
5 @# gets passed to the output verbatim:
6 Verbatim.
7
8 @# The "@" starts a control sequence. Arguments of control sequences
9 @# are either everything to the end of the current line (as in case
10 @# of the comment command you are looking at) balanced parenthesized
11 @# string.
12 @#
13
14 @# If we want to ignore and end of line character, just prefix it by "@":
15 This line @
16 won't be broken@
17 .
18
19 @# If you want to write the "@" character, just write it twice:
20 @@
21
22 @# You can embed any perl code in your text (use the "out" function to
23 @# write to the templater's output):
24 @[foreach $x (1..10) { out "$x "; } ]
25
26 @# Or if you use @{...}, then the value of the last expression executed
27 @# gets printed automatically:
28 @{"1+2+3 = " . (1+2+3)}
29
30 @# You can also interpolate perl variables in the T:: namespace by using @$variable:
31 I was run as @$0.
32
33 @# It is possible to call any perl functions
34 @# (any result other than undef will be printed automatically)
35 @# Use `out' to write to the output stream
36 @out("One", "Two", "Three\n")
37
38 @# Or do the same with parameters running up to the end of the line:
39 @out "Four", "Five", "Six\n"
40
41 @# Of course you can define your own functions (they live in the T:: namespace)
42 @[ sub quork($) { my ($arg) = @_;
43 return "Thus quorkth the Penguin: $arg";
44 } ]
45 @quork("404!")
46
47 @# There are also some conditionals (you can use any perl expressions):
48 @if 1
49 @if 0
50 one
51 @elif 0
52 two
53 @elif 1
54 three
55 @fi
56 @else
57 four
58 @fi
59
60 @# Or you can use the parenthesized form:
61 @if(0)ONE@else()TWO@fi()THREE
62
63 @# To include another file (and possibly change some variables), use:
64 @include("test2", "a" => "HOWDY", "b" => "HULLO", "c" => "AHOY")
65 @include "test2", "a" => "howdy", "b" => "hullo", "c" => "ahoy"
66
67 @# And to load a perl file:
68 @load "test3"
69
70 @# This one is tricky:
71 @out("z\n", <<EOF
72 a
73 b
74 c
75 EOF
76 )
77
78 @# The template engine can be run from any Perl code (including Perl embedded
79 @# in another template), e.g., in a templated CGI script.
80 @[
81         UCW::Temple::process_string('I still see I was run as @$0');
82 ]
83
84 @# Additionally, you can pass a hash of arguments to process_string() and use
85 @# the @(name) construct to expand them:
86 @[
87         UCW::Temple::process_string('@(count) upon a @(time) dreary...',
88                 { 'count' => 'Once', 'time' => 'midnight' } );
89 ]
90
91 @# We are done:
92 Finis.