NAME
Voodoo - Beware Of The Zombies!
SYNOPSIS
use Voodoo;
my $voodoo = Voodoo->new;
# Simple
print $voodoo->render(<<'EOF');
<html>
<head></head>
<body>
Time: <%= localtime(time) %>
</body>
</html>
EOF
# More complicated
print $voodoo->render(<<'EOF', 23, 'foo bar');
%= 5 * 5
% my ($number, $text) = @_;
test 123
foo <% my $i = $number + 2 %>
% for (1 .. 23) {
* some text <%= $i++ %>
% }
EOF
DESCRIPTION
Voodoo is a minimalistic and very Perl-ish template engine, designed specifically for all those small tasks that come up during big projects. Like preprocessing a config file, generating text from heredocs and stuff like that. For bigger tasks you might want to use HTML::Mason or Template.
<% Inline Perl %>
<%= Perl expression, replaced with result %>
<%# Comment, useful for debugging %>
% Perl line
%= Perl expression line, replaced with result
%# Comment line, useful for debugging
Voodoo templates work just like Perl subs (actually they get compiled to a Perl sub internally). That means you can access arguments simply via @_
.
% my ($foo, $bar) = @_;
% my $x = shift;
test 123 <%= $foo %>
Note that you can't escape Voodoo tags, instead we just replace them if neccessary.
my $voodoo = Voodoo->new;
$voodoo->line_start('@@');
$voodoo->tag_start('[@@');
$voodoo->tag_end('@@]');
$voodoo->expression_mark('&');
$voodoo->render(<<'EOF', 23);
@@ my $i = shift;
<% no code just text [@@& $i @@]
EOF
There is only one case that we can escape with a backslash, and thats a newline at the end of a template line.
This is <%= 23 * 3 %> a\
single line
If for some strange reason you absolutely need a backslash in front of a newline you can escape the backslash with another backslash.
% use Data::Dumper;
This will\\
result <%= Dumper {foo => 'bar'} %>\\
in multiple lines
Templates get compiled to Perl code internally, this can make debugging a bit tricky. But by setting the debug
attribute to 1
, you can tell Voodoo to trace all errors that might occur and present them in a very convenient way with context.
Ya Voodoo seem weak aroun line 4, mon.
-----------------------------------------------------------------
2: </head>
3: <body>
4: % my $i = 2; xx
5: %= $i * 2
6: </body>
-----------------------------------------------------------------
Bareword "xx" not allowed while "strict subs" in use at (eval 13)
line 4.
Voodoo does not support caching by itself, but you can easily build a wrapper around it.
# Compile and store code somewhere
my $voodoo = $voodoo->new;
$voodoo->parse($template);
$voodoo->compile;
my $code = $voodoo->code;
# Load code and template (template for debug trace only)
$voodoo->template($template);
$voodoo->code($code);
my $result = $voodoo->interpret(@arguments);
ATTRIBUTES
code
my $code = $voodoo->code;
$voodoo = $voodoo->code($code);
comment_mark
my $comment_mark = $voodoo->comment_mark;
$voodoo = $voodoo->comment_mark('#');
debug
my $debug = $voodoo->debug;
$voodoo = $voodoo->debug(1);
$voodoo = $voodoo->debug(2);
expression_mark
my $expression_mark = $voodoo->expression_mark;
$voodoo = $voodoo->expression_mark('=');
line_start
my $line_start = $voodoo->line_start;
$voodoo = $voodoo->line_start('%');
template
my $template = $voodoo->template;
$voodoo = $voodoo->template($template);
tree
my $tree = $voodoo->tree;
$voodoo = $voodoo->tree($tree);
tag_start
my $tag_start = $voodoo->tag_start;
$voodoo = $voodoo->tag_start('<%');
tag_end
my $tag_end = $voodoo->tag_end;
$voodoo = $voodoo->tag_end('%>');
METHODS
Voodoo inherits all methods from Nevermore and implements the following new ones.
new
my $voodoo = Voodoo->new;
compile
$voodoo = $voodoo->compile;
interpret
my $result = $voodoo->interpret;
my $result = $voodoo->interpret(@arguments);
parse
$voodoo = $voodoo->parse($template);
render
my $result = $voodoo->render($template);
my $result = $voodoo->render($template, @arguments);
renter_file
my $result = $voodoo->render($template_file);
my $result = $voodoo->render($template_file, @arguments);
renter_file_to_file
my $result = $voodoo->render($template_file, $result_file);
my $result = $voodoo->render($template_file, $result_file, @arguments);
renter_to_file
my $result = $voodoo->render($template, $result_file);
my $result = $voodoo->render($template, $result_file, @arguments);