NAME
Text::Haml - Haml Perl implementation
SYNOPSIS
use Text::Haml;
my $haml = Text::Haml->new;
my $html = $haml->render('%p foo'); # <p>foo</p>
$html = $haml->render('= user', user => 'friend'); # <div>friend</div>
DESCRIPTION
Text::Haml implements Haml http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html specification.
Text::Haml passes specification tests written by Norman Clarke http://github.com/norman/haml-spec and supports only cross-language Haml features. Do not expect Ruby specific things to work.
ATTRIBUTES
Text::Haml implements the following attributes:
format
Supported formats: xhtml, html, html5.
Default is xhtml.
encoding
Default is utf-8.
escape
Escape subroutine presented as string.
Default is
$haml->escape(<<'EOF');
my $s = shift;
$s =~ s/&/&/g;
$s =~ s/</</g;
$s =~ s/>/>/g;
$s =~ s/"/"/g;
$s =~ s/'/'/g;
return $s;
EOF
escape_html
Switch on/off Haml output html escaping.
Default is on.
vars_as_subs
When options is NOT SET (by default) passed variables are normal Perl variables and are use with $
prefix.
$haml->render('%p $var', var => 'hello');
When this option is SET passed variables are Perl lvalue subroutines and are used without $
prefix.
$haml->render('%p var', var => 'hello');
But if you declare Perl variable in a block, it must be used with $
prefix.
$haml->render('<<EOF')
- my $foo;
%p= $foo
EOF
helpers
Holds helpers subroutines. Helpers can be called in Haml text as normal Perl
functions. See also add_helper.
helpers => {
foo => sub {
my $self = shift;
my $string = shift;
$string =~ s/r/z/;
return $string;
}
}
helpers_arg
First argument passed to the helper.
$haml->helpers_args($my_context);
Deafault is Text::Haml instance.
error
Holds last error.
METHODS
new
my $haml = Text::Haml->new;
add_helper
Adds a new helper.
$haml->add_helper(current_time => sub { time });
add_filter
Adds a new filter.
$haml->add_filter(compress => sub { $_[0] =~ s/\s+/ /g; $_[0]});
render
Renders Haml string. Returns undef on error. See error attribute.
my $text = $haml->render('%p foo');
my $text = $haml->render('%p var', var => 'hello');
render_file
A helper method that loads a file and passes it to the render method.
my $text = $haml->render_file('foo.haml');
PERL SPECIFIC IMPLEMENTATION ISSUES
String interpolation
Despite of existing string interpolation in Perl, Ruby interpolation is also supported.
$haml->render('%p Hello #{user}', user => 'foo')
Hash keys
When declaring tag attributes :
symbol can be used.
$haml->render("%a{:href => 'bar'}");
Perl-style is supported but not recommented, since your Haml template won't work with Ruby Haml implementation parser.
$haml->render("%a{href => 'bar'}");
DEVELOPMENT
Repository
http://github.com/vti/text-haml/commits/master
AUTHOR
Viacheslav Tykhanovskyi, vti@cpan.org
.
CREDITS
In alphabetical order:
Norman Clarke
COPYRIGHT AND LICENSE
Copyright (C) 2009, Viacheslav Tykhanovskyi.
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.