1Text::Haml(3pm)       User Contributed Perl Documentation      Text::Haml(3pm)
2
3
4

NAME

6       Text::Haml - Haml Perl implementation
7

SYNOPSIS

9           use Text::Haml;
10
11           my $haml = Text::Haml->new;
12
13           my $html = $haml->render('%p foo'); # <p>foo</p>
14
15           $html = $haml->render('= $user', user => 'friend'); # <div>friend</div>
16
17           # Use Haml file
18           $html = $haml->render_file('tmpl/index.haml', user => 'friend');
19

DESCRIPTION

21       Text::Haml implements Haml
22       <http://haml.info/docs/yardoc/file.REFERENCE.html> specification.
23
24       Text::Haml passes specification tests written by Norman Clarke
25       https://github.com/haml/haml-spec and supports only cross-language Haml
26       features. Do not expect ruby or Rails specific extensions to work.
27

ATTRIBUTES

29       Text::Haml implements the following attributes:
30
31   "append"
32       Holds the string of code that is appended to the generated Perl code.
33
34   "code"
35       Holds the Perl code.
36
37   "compiled"
38       Holds compiled code.
39
40   "encoding"
41           $haml->encoding('utf-8');
42
43       Default is utf-8.
44
45   "escape"
46       Escape subroutine presented as string.
47
48       Default is
49
50           $haml->escape(<<'EOF');
51               my $s = shift;
52               return unless defined $s;
53               $s =~ s/&/&amp;/g;
54               $s =~ s/</&lt;/g;
55               $s =~ s/>/&gt;/g;
56               $s =~ s/"/&quot;/g;
57               $s =~ s/'/&apos;/g;
58               return $s;
59           EOF
60
61   "escape_html"
62           $haml->escape_html(0);
63
64       Switch on/off Haml output html escaping. Default is on.
65
66   "filters"
67       Holds filters.
68
69   "format"
70           $haml->format('xhtml');
71
72       Supported formats: xhtml, html, html5.
73
74       Default is xhtml.
75
76   "namespace"
77       Holds the namespace under which the Perl package is generated.
78
79   "prepend"
80       Holds the string of code that is prepended to the generated Perl code.
81
82   "vars"
83       Holds the variables that are passed during the rendering.
84
85   "vars_as_subs"
86       When options is NOT SET (by default) passed variables are normal Perl
87       variables and are used with "$" prefix.
88
89           $haml->render('%p $var', var => 'hello');
90
91       When this option is SET passed variables are Perl lvalue subroutines
92       and are used without "$" prefix.
93
94           $haml->render('%p var', var => 'hello');
95
96       But if you declare Perl variable in a block, it must be used with "$"
97       prefix.
98
99           $haml->render('<<EOF')
100               - my $foo;
101               %p= $foo
102           EOF
103
104   "helpers"
105           helpers => {
106               foo => sub {
107                   my $self   = shift;
108                   my $string = shift;
109
110                   $string =~ s/r/z/;
111
112                   return $string;
113               }
114           }
115
116       Holds helpers subroutines. Helpers can be called in Haml text as normal
117       Perl functions. See also add_helper.
118
119   "helpers_arg"
120           $haml->helpers_args($my_context);
121
122       First argument passed to the helper (Text::Haml instance by default).
123
124   "error"
125           $haml->error;
126
127       Holds the last error.
128
129   "tape"
130       Holds parsed haml elements.
131
132   "path"
133       Holds path of Haml templates. Current directory is a default.  If you
134       want to set several paths, arrayref can also be set up.  This way is
135       the same as Text::Xslate.
136
137   "cache"
138       Holds cache level of Haml templates. 1 is a default.  0 means "Not
139       cached", 1 means "Checked template mtime" and 2 means "Used always
140       cached".  This way is the same as Text::Xslate.
141
142   "cache_dir"
143       Holds cache directory of Haml templates. $ENV{HOME}/.text_haml_cache is
144       a default.  Unless $ENV{HOME}, File::Spec->tempdir was used.  This way
145       is the same as Text::Xslate.
146

METHODS

148   "new"
149           my $haml = Text::Haml->new;
150
151   "add_helper"
152           $haml->add_helper(current_time => sub { time });
153
154       Adds a new helper.
155
156   "add_filter"
157           $haml->add_filter(compress => sub { $_[0] =~ s/\s+/ /g; $_[0]});
158
159       Adds a new filter.
160
161   "build"
162           $haml->build(@_);
163
164       Builds the Perl code.
165
166   "compile"
167           $haml->compile;
168
169       Compiles parsed code.
170
171   "interpret"
172           $haml->interpret(@_);
173
174       Interprets compiled code.
175
176   "parse"
177           $haml->parse('%p foo');
178
179       Parses Haml string building a tree.
180
181   "render"
182           my $text = $haml->render('%p foo');
183
184           my $text = $haml->render('%p var', var => 'hello');
185
186       Renders Haml string. Returns undef on error. See error attribute.
187
188   "render_file"
189           my $text = $haml->render_file('foo.haml', var => 'hello');
190
191       A helper method that loads a file and passes it to the render method.
192       Since "%____vars" is used internally, you cannot use this as parameter
193       name.
194

PERL SPECIFIC IMPLEMENTATION ISSUES

196   String interpolation
197       Despite of existing string interpolation in Perl, Ruby interpolation is
198       also supported.
199
200       $haml->render('%p Hello #{user}', user => 'foo')
201
202   Hash keys
203       When declaring tag attributes ":" symbol can be used.
204
205       $haml->render("%a{:href => 'bar'}");
206
207       Perl-style is supported but not recommented, since your Haml template
208       won't work with Ruby Haml implementation parser.
209
210       $haml->render("%a{href => 'bar'}");
211
212   Using with Data::Section::Simple
213       When using the Data::Section::Simple, you need to unset the variable
214       "encoding" in the constructor or using the "encoding" attribute of the
215       Text::Haml:
216
217           use Data::Section::Simple qw/get_data_section/;
218           my $vpath = get_data_section;
219
220           my $haml = Text::Haml->new(cache => 0, path => $vpath, encoding => '');
221           # or
222           #my $haml = Text::Haml->new(cache => 0, path => $vpath);
223           #$haml->encoding(''); # encoding attribute
224
225           my $index = $haml->render_file('index.haml');
226           say $index;
227
228           __DATA__
229
230           @@ index.haml
231           %strong XXXXX
232
233       see <https://metacpan.org/pod/Data::Section::Simple#utf8-pragma>
234

DEVELOPMENT

236   Repository
237           http://github.com/vti/text-haml
238

AUTHOR

240       Viacheslav Tykhanovskyi, "vti@cpan.org".
241

CREDITS

243       In order of appearance:
244
245       Nick Ragouzis
246
247       Norman Clarke
248
249       rightgo09
250
251       Breno G. de Oliveira (garu)
252
253       Yuya Tanaka
254
255       Wanradt Koell (wanradt)
256
257       Keedi Kim
258
259       Carlos Lima
260
261       Jason Younker
262
263       TheAthlete
264
265       Mark Aufflick (aufflick)
266
267       Graham Todd (grtodd)
268
270       Copyright (C) 2009-2017, Viacheslav Tykhanovskyi.
271
272       This program is free software, you can redistribute it and/or modify it
273       under the terms of the Artistic License version 2.0.
274
275
276
277perl v5.30.0                      2019-07-26                   Text::Haml(3pm)
Impressum