forked from perlpunk/HTML-Template-Compiled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
1748 lines (1295 loc) · 59.1 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
NAME
HTML::Template::Compiled - Template System Compiles HTML::Template files
to Perl code
VERSION
$VERSION = "1.001_001"
SYNOPSIS
use HTML::Template::Compiled;
# recommended options:
# case_sensitive => 1
# search_path_on_include => 1
# use_query => 0
# default_escape => 'HTML' # <-- HIGHLY RECOMMENDED
# note that the following
# use HTML::Template::Compiled speed => 1
# is deprecated (can be problematic under persistent environments)
# or for the biggest compatibility with HTML::Template
# case_sensitive => 0
# search_path_on_include => 0
# use_query => 1
# note that the following
# use HTML::Template::Compiled compatible => 1;
# is deprecated (can be problematic under persistent environments)
# or use HTML::Template::Compiled::Classic
my $htc = HTML::Template::Compiled->new(
filename => 'test.tmpl',
case_sensitive => 1,
default_escape => 'HTML',
);
$htc->param(
BAND => $name,
ALBUMS => [
{ TITLE => $t1, YEAR => $y1 },
{ TITLE => $t2, YEAR => $y2 },
],
);
print $htc->output;
test.tmpl:
Band: <TMPL_VAR BAND>
<TMPL_LOOP ALBUMS>
Title: <TMPL_VAR TITLE> (<TMPL_VAR YEAR>)
</TMPL_LOOP>
Or use different tag styles:
Band: <%= BAND %>
<%loop ALBUMS %>
Title: <%= TITLE %> (<%= YEAR %>)
<%/loop %>
Band: [%= BAND %]
[%loop ALBUMS %]
Title: [%= TITLE %] ([%= YEAR %])
[%/loop %]
DESCRIPTION
HTML::Template::Compiled is a template system which can be used for
HTML::Template templates with almost the same API. It offers more
flexible template delimiters, additional tags and features, and by
compiling the template into perl code it can run significantly faster in
persistent environments such as FastCGI or mod_perl.
The goal is to offer more features for flexibility but keep the basic
syntax as easy as it is.
Features at a glance:
Dot notation for objects, hashes and arrays
Use expressions without any disadvantages like those in
HTML::Template::Expr
Write escaping plugins and plugins for new tags
Alternate delimiters, e.g. "[%if %]" and "<%if %>"
Avoid "global_vars" option by using the "SET_VAR" tag to create aliases.
Tags ELSIF, EACH, WHILE, COMMENT, WRAPPER, SWITCH/CASE, INCLUDE_VAR
Chomp newlines/whitespace
For a quick reference, see HTML::Template::Compiled::Reference.
As the basic features work like in HTML::Template, please get familiar
with its documentation before.
HTML::Template::Compiled (HTC) does not implement all features of
HTML::Template (see "COMPATIBILITY"), and it has got some additional
features which are explained below: "ADDITIONAL FEATURES"
See "BENCHMARKS" for some examples on the performance. Since it depends
highly on the options used and on the template size there can be no
general statement on its performance.
You might want to use HTML::Template::Compiled::Lazy for CGI
environments as it doesn't parse the template before calling output. But
note that HTC::Lazy isn't much tested, and I don't use it myself, so
there's a lack of experience. If you use it and have problems, please
report.
HTC will use a lot of memory because it keeps all template objects in
memory. If you are on mod_perl, and have a lot of templates, you should
preload them at server startup to be sure that it is in shared memory.
At the moment HTC is not fully tested for keeping all data in shared
memory (e.g. when a copy-on-write occurs), but it seems like it's
behaving well. For preloading you can use
HTML::Template::Compiled->preload($cache_dir).
Generating code, writing it on disk and later eval() it can open
security holes, for example if you have more users on the same machine
that can access the same files (usually an http server running as 'www'
or 'nobody'). See "SECURITY" for details what you can do to safe
yourself.
NOTE: If you don't need any of the additional features listed below and
if you don't need the speed (in many cases it's probably not worth
trading speed for memory), then you might be better off with just using
HTML::Template.
NOTE2: If you have any questions, bug reports, send them to me and not
to Sam Tregar. This module is developed by me at the moment,
independently from HTML::Template, although I try to get most of the
tests from it passing for HTC. See "RESOURCES" for current information.
FEATURES FROM HTML::TEMPLATE
TMPL_VAR
TMPL_LOOP
TMPL_(IF|UNLESS|ELSE)
TMPL_INCLUDE
HTML_TEMPLATE_ROOT
ESCAPE=(HTML|URL|JS|0)
DEFAULT=...
"__first__", "__last__", "__inner__", "__outer__", "__odd__",
"__counter__", "__even__"
<!-- TMPL_VAR NAME=PARAM1 -->
case insensitive var names
use option case_sensitive => 0 to use this feature (slow down)
filters
vars that are subrefs - not implemented, only in
HTML::Template::Compiled::Classic
scalarref, arrayref, filehandle
"global_vars"
"query"
Has a bug (doesn't return parameters in included files of included
files). I'm working on that.
ADDITIONAL FEATURES
What can HTC do for you additionally to HTML::Template?
tag TMPL_ELSIF
No need to have cascading "if-else-if-else"s
tag TMPL_EACH
Iterate over a hash. See "TMPL_EACH"
tag TMPL_WITH
see "TMPL_WITH"
tag TMPL_WHILE
see "TMPL_WHILE"
tag TMPL_SET_VAR
see "SET_VAR"
tag TMPL_USE_VARS
see "USE_VARS"
tags TMPL_COMMENT, TMPL_NOPARSE, TMPL_VERBATIM
see "TMPL_COMMENT", "TMPL_NOPARSE", "TMPL_VERBATIM"
tag TMPL_WRAPPER
see "WRAPPER"
"__index__"
Additional loop variable ("__counter__ -1")
"__break__"
Additional loop variable (see "TMPL_LOOP")
"__filename__", "__filenameshort__" (since 0.91_001)
Insert the template filename for debugging:
<%= __filename__ %>
<%= __filenameshort__ %>
will turn out as: templates/path/file.html path/file.html
See also option debug_file in "OPTIONS" for adding the filename
globally.
tags TMPL_SWITCH, TMPL_CASE
see "TMPL_SWITCH"
"TMPL_PERL"
Include perl code in your template. See "RUNNING PERL WITH
TMPL_PERL"
CHOMP
New in version 0.96_001, please report any bugs and send me
suggestions.
You can set global chomp options in the constructor. These work like
in Template-Toolkit:
my $htc = HTML::Template::Compiled->new(
pre_chomp => 0, # 0, 1, 2, 3, default 0
post_chomp => 1, # 0, 1, 2, 3, default 0
);
Meaning of the values: 0: Don't chomp 1: remove only spaces in the
line before or after the tag 2: remove all whitespaces before or
after the tag, and replace with one space 3: remove all whitespaces
before or after the tag
In the template you can change that feature by using PRE_CHOMP and
POST_CHOMP attributes:
<%= foo PRE_CHOMP=3 POST_CHOMP=1 %>
The experimental tags +..._chomp have been removed.
Generating perl code
See "IMPLEMENTATION"
better variable access
dot-notation for accessing hash values. See "EXTENDED VARIABLE
ACCESS"
rendering objects
dot-notation for accessing object methods. See "RENDERING OBJECTS"
output to filehandle
See "OPTIONS"
Dynamic includes
"INCLUDE_VAR", "INCLUDE_STRING". See "INCLUDE"
tag TMPL_IF_DEFINED
Check for definedness instead of truth: <TMPL_IF_DEFINED NAME="var">
ALIAS
Set an alias for a loop variable. You can use the alias then with
$alias. The syntax without the "$" is also possible but not
recommended any more.
For example, these two loops are functionally equivalent:
<tmpl_loop foo>
<tmpl_var _>
</tmpl_loop foo>
<tmpl_loop foo alias=current>
<tmpl_var $current>
</tmpl_loop foo>
This works with "TMPL_LOOP" and "TMPL_WHILE" at the moment.
You can also set aliases with the "SET_VAR" tag. See "SET_VAR"
To use template parameters with a "$" at the beginning (which is not
officially supported, but some are obviously using it), you can set:
local $HTML::Template::Compiled::Compiler::DISABLE_NEW_ALIAS = 1;
This is only a temporary workaround and will be removed some day!
Note that you are also able to access variables with dollar signs
like this:
<tmpl_var _.$foo >
since underscore means current position in the parameter stash, and
aliases are only recognized at the beginning of a template var. But
note that dollar signs are still not officially supported.
Chained escaping
See "ESCAPING"
tagstyles
For those who like it (i like it because it is shorter than TMPL_),
you can use <% %> tags and the <%= tag instead of <%VAR (which will
work, too):
<%IF blah%> <%= VARIABLE%> <%/IF%>
Define your own tagstyles and/or deactivate predefined ones. See
"OPTIONS" tagstyle.
pre_chomp, post_chomp
See "CHOMP"
MISSING AND DIFFERENT FEATURES
There are some features of H::T that are missing or behaving different.
I'll try to list them here.
MISSING FEATURES
die_on_bad_params
I don't think I'll implement that.
force_untaint
Not planned at the moment
vanguard_compatibility_mode
Not planned.
shared_cache, double_cache
Not planned at the moment
blind_cache
Not sure if I should implement. In HTC you have the possibility to
set the expire time of the templates (after that time in memory the
template file is rechecked if it has changed), so setting a very
high value for expire_time would have the same effect as
blind_cache. See "CACHING" "expire_time"
double_file_cache
If I understand correctly, in HT, this enables memory and file cache
at the same time. In HTC, this is not needed. If you use file_cache
and cache, both are used.
file_cache_dir_mode
Not planned. The cache dir must exist, and subdirectories are not
created at the moment.
cache_lazy_vars, cache_lazy_loops
Not planned at the moment (This would be for
HTML::Template::Compiled::Classic, since it implements code refs).
utf8
Might be added in the future, HTC already has "open_mode"
various debug options
Might be implemented in the future
associate
Not planned.
max_includes
Not planned
die_on_missing_include
Maybe
DIFFERENT FEATURES
case_sensitive
default is 1 (on).
Deactivate by passing option case_sensitive 0.
Note (again): this will slow down templating a lot (50%).
Explanation: This has nothing to do with "TMPL_IF" or "tmpl_if".
It's about the variable names. With case_sensitive set to 1, the
following tags are different:
<tmpl_var Foo> prints the value of hash key 'Foo'
<tmpl_var fOO> prints the value of hash key 'fOO'
With case_sensitive set to 0, all your parameters passed to
"param()" are converted to lowercase, and the following tags are the
same:
<tmpl_var Foo> prints the value of hash key 'foo'
<tmpl_var fOO> prints the value of hash key 'foo'
subref variables
As of version 0.69, subref variables are not supported any more with
HTML::Template::Compiled. Use HTML::Template::Compiled::Classic
(contained in this distribution) instead. It provides most features
of HTC.
search_path_on_include
Default: 0
In the HTML::Template documentation it says, if
search_path_on_include is set to 1, the paths of the path option are
searched, while the default behaviour is to look "only" in the
current template directory.
It's not clear if it still searches in the current directory if set
to 1. I found out that it is not, so you cannot have both.
In HTML::Template::Compiled, search_path_on_include can have three
values: 0: search current template directory 1: search paths
specified 2: search paths and current template directory.
open_mode
In HTC you should leave out the "<" at the beginning.
If you want to have your templates read in utf-8, use
open_mode => ':encoding(utf-8)',
as an option.
use_query
default is 0 (off). Set it via the option "use_query"
Arrayrefs
At the moment this snippet
<tmpl_if arrayref>true<tmpl_else>false</tmpl_if arrayref>
with this code:
$htc->param(arrayref => []);
will print true in HTC and false in HTML::Template. In
HTML::Template an array is true if it has content, in HTC it's true
if it (the reference) is defined. I'll try to find a way to change
that behaviour, though that might be for the cost of speed.
As of HTML::Template::Compiled 0.85 you can use this syntax:
<tmpl_if arrayref# >true<tmpl_else>false</tmpl_if >
In HTML::Template::Compiled::Classic 0.04 it works as in
HTML::Template.
debug_cache
Additional to 0 or 1 it can take an array ref for debugging only
specific cache operations.
Note: the following is deprecated:
To be compatible in all of the above options all use:
use HTML::Template::Compiled compatible => 1;
If you don't care about these options you should use
use HTML::Template::Compiled speed => 1;
which is the default but depending on user wishes that might change.
DEPRECATED
class methods ExpireTime, EnableSub, CaseSensitive, SearchPathOnInclude,
UseQuery
option formatter_path
tag USE_VARS, not needed anymore
option cache_dir (replaced by file_cache_dir)
options method_call, deref, default_path, dumper
import tags short, compatible, speed
ESCAPING
Like in HTML::Template, you have "ESCAPE=HTML", "ESCAPE=URL" and
"ESCAPE_JS". "ESCAPE=HTML" will only escape '"&<>. If you want to escape
more, use "ESCAPE=HTML_ALL". Additionally you have "ESCAPE=DUMP", which
by default will generate a Data::Dumper output.
You can also chain different escapings, like "ESCAPE=DUMP|HTML".
Additionally to ESCAPE=JS you have ESCAPE=IJSON which does not escape
the single quote.
INCLUDE
Additionally to
<TMPL_INCLUDE NAME="file.htc">
you can do an include of a template variable:
<TMPL_INCLUDE_VAR NAME="file_include_var">
$htc->param(file_include_var => "file.htc");
Using "INCLUDE VAR="..."" is deprecated.
You can also include strings:
template:
inc: <%include_string foo %>
code:
$htc->param(
foo => 'included=<%= bar%>',
bar => 'real',
);
output:
inc: included=real
Note that included strings are not cached and cannot include files or
strings themselves.
EXTENDED VARIABLE ACCESS
With HTC, you have more control over how you access your template
parameters. An example:
my %hash = (
SELF => '/path/to/script.pl',
LANGUAGE => 'de',
BAND => 'Bauhaus',
ALBUMS => [
{
NAME => 'Mask',
SONGS => [ { NAME => 'Hair of the Dog' }, ... ],
},
],
INFO => {
BIOGRAPHY => '...',
LINK => '...'
},
NAME => "Cool script",
);
Now in the TMPL_LOOP "ALBUMS" you would like to access the path to your
script, stored in $hash{SELF}. in HTML::Template you have to set the
option "global_vars", so you can access $hash{SELF} from everywhere.
Unfortunately, now "NAME" is also global, which might not a problem in
this simple example, but in a more complicated template this is
impossible. With HTC, you wouldn't use "global_vars" here, but you can
say:
<TMPL_VAR .SELF>
to access the root element, and you could even say ".INFO.BIOGRAPHY" or
"ALBUMS[0].SONGS[0].NAME" (the latter has changed since version 0.79)
RENDERING OBJECTS
This is still in development, so I might change the API here.
Additionally to feeding a simple hash to HTC, you can feed it objects.
To do method calls you can also use '.' in the template.
my $htc = HTML::Template::Compiled->new(
...
);
$htc->param(
VAR => "blah",
OBJECT => bless({...}, "Your::Class"),
);
<TMPL_VAR NAME="OBJECT.fullname">
<TMPL_WITH OBJECT>
Name: <TMPL_VAR fullname>
</TMPL_WITH>
"fullname" will call the fullname method of your Your::Class object.
It's recommended to just use the default . value for methods and
dereferencing.
I might stop supporting that you can set the values for method calls by
setting an option. Ideally I would like to have that behaviour changed
only by inheriting.
RUNNING PERL WITH TMPL_PERL
Yes, templating systems are for separating code and templates. But as it
turned out to be implemented much easier than expressions i decided to
implement it. But expressions are also available with the option
"use_expressions".
Note: If you have templates that can be edited by untrustworthy persons
then you don't want them to include perl code.
So, how do you use the perl-tag? First, you have to set the option
"use_perl" to 1 when creating a template object.
Important note: don't use "print" in the included code. Usually the
template code is concatenated and returned to your perl script. To
'print' something out use
__OUT__ 2**3;
This will be turned into something like
$OUT .= 2**3;
# or
print $fh 2**3;
Important note 2: HTC does not parse Perl. if you use the classic
tag-delimiters like this:
<tmpl_perl if (__CURRENT__->count > 42) { >
this will not work as it might seem. Use other delimiters instead:
<%perl if (__CURRENT__->count > 42) { %>
Example:
<tmpl_loop list>
<tmpl_perl unless (__INDEX__ % 3) { >
</tr><tr>
<tmpl_perl } >
</tmpl_loop list>
# takes the current position of the parameter
# hash, key 'foo' and multiplies it with 3
<%perl __OUT__ __CURRENT__->{foo} * 3; %>
List of special keywords inside a perl-tag:
__OUT__
Is turned into "$OUT .=" or "print $fh"
__HTC__
Is turned into the variable containing the current template object.
__CURRENT__
Turned into the variable containing the current position in the
parameter hash.
__ROOT__
Turned into the variable containig the parameter hash.
__INDEX__
Turned into the current index of a loop (starting with 0).
INHERITANCE
It's possible since version 0.69 to inherit from
HTML::Template::Compiled. It's just not documented, and internal method
names might change in the near future. I'll try to fix the API and
document which methods you can inherit.
METHODS TO INHERIT
method_call
Default is "sub method_call { '.' }"
deref
Default is "sub deref { '.' }"
formatter_path
Deprecated, see HTML::Template::Compiled::Formatter please.
compile_early
Define if every included file should be checked and parsed at
compile time of the including template or later when it is really
used.
Default is "sub compile_early { 1 }"
parser_class
Default is "sub parser_class { 'HTML::Template::Compiled::Parser' }"
You can write your own parser class (which must inherit from
HTML::Template::Compiled::Parser) and use this.
HTML::Template::Compiled::Lazy uses this.
DEBUGGING
For printing out the contents of all the parameters you can do:
<TMPL_LOOP ALBUMS>
Dump: <TMPL_VAR _ ESCAPE=DUMP|HTML>
</TMPL_LOOP>
The special name "_" gives you the current parameter and "ESCAPE=DUMP"
will by default generate a Data::Dumper output of the current variable,
in this case it will dump out the contents of every album in a loop. To
correctly display that in html "|HTML" will escape html entities.
TMPL_WITH
If you have a deep leveled hash you might not want to always write
THE.FULL.PATH.TO.YOUR.VAR. Jump to your desired level once and then you
need only one level. Compare:
<TMPL_WITH DEEP.PATH.TO.HASH>
<TMPL_VAR NAME>: <TMPL_VAR AGE>
</TMPL_WITH>
<TMPL_VAR DEEP.PATH.TO.HASH.NAME>: <TMPL_VAR DEEP.PATH.TO.HASH.AGE>
Inside TMPL_WITH you can't reference parent nodes unless you're using
global_vars.
TMPL_LOOP
The special name "_" gives you the current parameter. In loops you can
use it like this:
<tmpl_loop foo>
Current item: <tmpl_var _ >
</tmpl_loop>
Also you can give the current item an alias. See "ALIAS".
The LOOP tag allows you to define a JOIN attribute:
<tmpl_loop favourite_colors join=", ">
<tmpl_var _ >
</tmpl_loop>
This will output something like "blue, pink, yellow". This is easier
than doing:
<tmpl_loop favourite_colors>
<tmpl_unless __first__>, </tmpl_unless>
<tmpl_var _ >
</tmpl_loop>
The "LOOP", "WHILE" and "EACH" tags allow you to define a BREAK
attribute:
<tmpl_loop bingo break="3"> <tmpl_var _ ><if __break__>\n</if></tmpl_loop>
$htc->param(bingo => [qw(X 0 _ _ X 0 _ _ X)]);
outputs
X 0 _
_ X 0
_ _ X
So specifying BREAK=3 sets __break__ to 1 every 3rd loop iteration.
TMPL_LOOP expects an array reference, also if it is a method call. If
you want to iterate with TMPL_LOOP over a list from a method call, set
the attribute "context=list":
<tmpl_loop object.list_method context=list>
<tmpl_var _ >
</tmpl_loop>
TMPL_WHILE
Useful for iterating, for example over database resultsets. The
directive
<tmpl_while resultset.fetchrow>
<tmpl_var _.0>
</tmpl_while>
will work like: while (my $row = $resultset->fetchrow) { print
$row->[0]; }
So the special variable name _ is set to the current item returned by
the iterator.
You also can use "ALIAS" here.
TMPL_EACH
Iterating over a hash. Internally it is not implemented as an each, so
you can also sort the output:
Sorted alphanumerically by default (since 0.93):
<tmpl_each letters >
<tmpl_var __key__ >:<tmpl_var __value__>
</tmpl_each letters >
Sorted numerically:
<tmpl_each numbers sort=num >
<tmpl_var __key__ >:<tmpl_var __value__>
</tmpl_each numbers >
Not sorted:
<tmpl_each numbers sort=0 >
<tmpl_var __key__ >:<tmpl_var __value__>
</tmpl_each numbers >
Sorted alphanumerically:
<tmpl_each letters sort=alpha >
<tmpl_var __key__ >:<tmpl_var __value__>
</tmpl_each letters >
You have to set the option "loop_context_vars" to true to use the
special vars "__key__" and "__value__".
If you want to iterate over a hash instead of a hashref (some methods
might return plain hashes instead of references and TMPL_EACH expects a
ref), then you can set "context=list":
<tmpl_each object.hash_method context=list>
<tmpl_var __key__ >
</tmpl_each>
Since 1.000_001 you can also define by which variable you want to sort.
If you have a hash with hashes as values:
$htc->param(
letters => {
1 => { letter =>'b' },
2 => { letter =>'a' },
3 => { letter =>'c' },
},
);
<%each letters sort=alpha sortby="letter" %>
<%set_var val value=__value__ %>
<%= __key__ %> = <%= $val.letter %>
<%/each%>
SET_VAR
Since 0.96_002
Sets a local variable to the value given in "value" or "expr"
<tmpl_set foo expr=23>
<tmpl_set name=bar expr=23>
<tmpl_set boo value=var.boo>
<tmpl_set oof expr="21*2">
<tmpl_var $foo>
<tmpl_var $bar>
...
"value=.." behaves like a variable name from the parameter stash. The
variable name to set must match /[0-9a-z_]+/i
You can refer to an alias via $alias or simply "alias". Note that the
latter syntax is not recommeded any more since it can conflict with
parameters from the stash.
If you want to use aliases in includes, you need to use the $alias
syntax.
USE_VARS
deprecated. Was added in 0.96_004 to make it possible to use aliases set
with "alias=..." or "SET_VAR" in includes. Now you should rather use the
<$alias> syntax.
The following explanation is just there for history and will be removed
some time in the future. For now it still works.
Necessary if you want vars like SET_VAR and loop aliases from outside in
includes. Before the first use in the include, add:
<tmpl_use_vars foo,bar,boo >
so that the compiler recognizes them as user defined vars and not
parameters from the stash. This statement is valid until the end of the
template so you cannot "overwrite" parameters of the stash locally.
WRAPPER
Since 0.97_005. Experimental. Please test.
Needs option "loop_context_vars".
Works similar to WRAPPER in Template-Toolkit.
Is similar to TMPL_INCLUDE, just that the included wrapper is wrapped
around the content. It can be used to avoid including head and foot
separately.
<tmpl_wrapper wrapper.html >
content: some var: <tmpl_var foo >
</tmpl_wrapper>
In wrapper.html the special loop context var "__wrapper__" is used for
the included content:
wrapper.html:
<some><layout>
<tmpl_var __wrapped__ >
</layout></some>
Important notes:
If you are using "out_fh" to print directly to a filehandle instead of
returning to a string, this feature might not be useful, since it is
appending the content inside of the wrapper to a string and prints it
when it comes to the end of the wrapper tag. So if you are using
"out_fh" to avoid generating long strings in memory, you should rather
use TMPL_INCLUDE instead.
Also you need perl 5.8 or higher to use it in combination with out_fh.
TMPL_COMMENT
For debugging purposes you can temporarily comment out regions:
Wanted: <tmpl_var wanted>
<tmpl_comment outer>
this won't be printed
<tmpl_comment inner>
<tmpl_var unwanted>
</tmpl_comment inner>
<tmpl_var unwanted>
</tmpl_comment outer>
$htc->param(unwanted => "no thanks", wanted => "we want this");
The output is (whitespaces stripped):
Wanted: we want this
HTC will ignore anything between COMMENT directives. This is useful for
debugging, and also for documentation inside the template which should
not be outputted.
TMPL_NOPARSE
Anything between
<tmpl_noparse>...</tmpl_noparse>
will not be recognized as template directives. Same syntax as
TMPL_COMMENT. It will output the content, though.
TMPL_VERBATIM
Anything between
<tmpl_verbatim>...</tmpl_verbatim>
will not be recognized as template directives. Same syntax as
"TMPL_NOPARSE", but it will be HTML-Escaped. This can be useful for
debugging.
TMPL_SWITCH
The SWITCH directive has the same syntax as VAR, IF etc. The CASE
directive takes a simple string or a comma separated list of strings.
Yes, without quotes. This will probably change! I just don't know yet
how it should look like. Suggestions?
With that directive you can do simple string comparisons.
<tmpl_switch language>(or <tmpl_switch name=language>)
<tmpl_case de>echt cool
<tmpl_case en>very cool
<tmpl_case es>superculo
<tmpl_case fr,se>don't speak french or swedish
<tmpl_case default>sorry, no translation for cool in language <%=language%> available
<tmpl_case>(same as default)
</tmpl_switch>
It's also possible to specify the default with a list of other strings:
<tmpl_case fr,default>
Note that the default case should always be the last statement before
the closing switch.
OPTIONS
As you can cache the generated perl code in files, some of the options
are fixed; that means for example if you set the option case_sensitive
to 0 and the next time you call the same template with case_sensitive 1
then this will be ignored. The options below will be marked as (fixed).
path
Path to template files
search_path_on_include
Search the list of paths specified with "path" when including a
template. Default is 0
See "DIFFERENT FEATURES" for the additional possible value 2.
file_cache
Set to 1 if you want to use file caching and specify the path with
file_cache_dir.
file_cache_dir
Path to caching directory (you have to create it before)
cache_dir
Replaced by file_cache_dir like in HTML::Template. Will be
deprecated in future versions.
cache
Is 1 by default. If set to 0, no memory cacheing is done. Only
recommendable if you have a dynamic template content (with
scalarref, arrayre for example).
expire_time
Recheck template files on disk after "expire_time" seconds. See
"CACHING"
filename
Template to parse
scalarref
Reference to a scalar with your template content. It's possible to
cache scalarrefs, too, if you have Digest::MD5 installed. Note that
your cache directory might get filled with files from earlier
versions. Clean the cache regularly.
Don't cache scalarrefs if you have dynamic strings. Your memory
might get filled up fast! Use the option
cache => 0
to disable memory caching.
arrayref
Reference to array containing lines of the template content
(newlines have to be included)
filehandle
Filehandle which contains the template content. Note that HTC will
not cache templates created like this.
loop_context_vars (fixed)
Vars like "__first__", "__last__", "__inner__", "__odd__",
"__counter__", "__index__", "__outer__", "__even__"
The variable "__index__" works just like "__counter__", only that it
starts at 0 instead of 1.
global_vars (fixed)
If set to 1, every outer variable can be accessed from anywhere in
the enclosing scope.
Default is 0.
Note that I don't recommend using global_vars. For referring to
parameters up in the stash you can use aliases via "alias=..." or
"SET_VAR". See "ALIAS" and "SET_VAR".
If yoy still would like to be able to navigate up the parameter
stash, you have the following option:
If set to 2, you don't have global vars, but have the possibility to
go up the stack one level. Example:
<tmpl_var ...key>
This will get you up 2 levels (remember: one dot means root in HTC)
and access the 'key' element.