documentationify
[scpubgit/HTML-String.git] / lib / HTML / String / TT.pm
CommitLineData
ed99cbb4 1package HTML::String::TT;
2
3use strictures 1;
51eaef0b 4
586054e0 5BEGIN {
6 if ($INC{"Template.pm"} and !$INC{"UNIVERSAL/ref.pm"}) {
7 warn "Template was loaded before we could load UNIVERSAL::ref"
8 ." - this means you're probably going to get weird errors."
9 ." To avoid this, use HTML::String::TT before loading Template."
10 }
11 require UNIVERSAL::ref;
12}
13
51eaef0b 14use HTML::String;
15use HTML::String::TT::Directive;
16use Safe::Isa;
ed99cbb4 17use Template;
18use Template::Parser;
19use Template::Stash;
ed99cbb4 20
21sub new {
22 shift;
23 Template->new(
24 PARSER => Template::Parser->new(
25 FACTORY => 'HTML::String::TT::Directive'
26 ),
27 STASH => Template::Stash->new,
51eaef0b 28 FILTERS => { no_escape => sub {
29 $_[0]->$_isa('HTML::String::Value')
30 ? HTML::String::Value->new(map $_->[0], @{$_[0]->{parts}})
31 : HTML::String::Value->new($_)
32 } },
77724961 33 (ref($_[0]) eq 'HASH' ? %{$_[0]} : @_)
ed99cbb4 34 );
35}
36
371;
d86bdf82 38
39__END__
40
41=head1 NAME
42
43HTML::String::TT - HTML string auto-escaping for L<Template Toolkit|Template>
44
45=head1 SYNOPSIS
46
47 my $tt = HTML::String::TT->new(\%normal_tt_args);
48
49or, if you're using L<Catalyst::View::TT>:
50
51 use HTML::String::TT; # needs to be loaded before TT to work
52
53 __PACKAGE__->config(
54 CLASS => 'HTML::String::TT',
55 );
56
57Then, in your template -
58
59 <h1>
60 [% title %] <-- this will be automatically escaped
61 </h1>
62 <div id="main">
63 [% some_html | no_escape %] <-- this won't
64 </div>
65 [% html_var = '<foo>'; html_var %] <-- this won't anyway
66
67(but note that the C<content> key in wrappers shouldn't need this).
68
69=head1 DESCRIPTION
70
71L<HTML::String::TT> is a wrapper for L<Template Toolkit|Template> that
72installs the following overrides:
73
74=over 4
75
76=item * The directive generator is replaced with
77L<HTML::String::TT::Directive> which ensures L<HTML::String::Overload> is
78active for the template text.
79
80=item * The stash is forced to be L<Template::Stash> since
81L<Template::Stash::XS> gets utterly confused if you hand it an object.
82
83=item * A filter C<no_escape> is added to mark outside data that you don't
84want to be escaped.
85
86=back
87
88The override happens to B<all> of the plain strings in your template, so
89even things declared within directives such as
90
91 [% html_var = '<h1>' %]
92
93will not be escaped, but any string coming from anywhere else will be. This
94can be a little bit annoying when you then pass it to things that don't
95respond well to overloaded objects, but is essential to L<HTML::String>'s
96policy of "always fail closed" - I'd rather it throws an exception than
97lets a value through unescaped, and if you care about your HTML not having
98XSS (cross site scripting) vulnerabilities then I hope you'll agree.
99
100We mark a number of TT internals namespaces as "don't escape when called by
101these", since TT has a tendency to do things like
102
103 open FH, "< $name";
104
105which really don't work if it gets converted to C<&quot; $name> while you
106aren't looking.
107
108Additionally, since TT often calls C<ref> to decide e.g.
109if something is a string or a glob, it's important that L<UNIVERSAL::ref>
110is loaded before TT is. We check to see if the latter is loaded and the
111former not, and warn loudly that you're probably going to get weird errors.
112
113This warning is not joking. "Probably" is optimistic. Load this module first.
114
115=head1 FILTERS
116
117=head2 no_escape
118
119The C<no_escape> filter marks the filtered input to not be escaped,
120so that you can provide HTML chunks from externally and still render them
121within the TT code.
122
123=head1 AUTHORS
124
125See L<HTML::String> for authors.
126
127=head1 COPYRIGHT AND LICENSE
128
129See L<HTML::String> for the copyright and license.
130
131=cut