Text::Handlebars - http://handlebarsjs.com/ for Text::Xslate
version 0.03
use Text::Handlebars; my $handlebars = Text::Handlebars->new( helpers => { fullName => sub { my ($context, $person) = @_; return $person->{firstName} . ' ' . $person->{lastName}; }, }, ); my $vars = { author => { firstName => 'Alan', lastName => 'Johnson' }, body => "I Love Handlebars", comments => [ author => { firstName => 'Yehuda', lastName => 'Katz' }, body => "Me too!", ], }; say $handlebars->render_string(<<'TEMPLATE', $vars); <div class="post"> <h1>By {{fullName author}}</h1> <div class="body">{{body}}</div> <h1>Comments</h1> {{#each comments}} <h2>By {{fullName author}}</h2> <div class="body">{{body}}</div> {{/each}} </div> TEMPLATE
produces
<div class="post"> <h1>By Alan Johnson</h1> <div class="body">I Love Handlebars</div> <h1>Comments</h1> <h2>By Yehuda Katz</h2> <div class="body">Me Too!</div> </div>
This module subclasses Text::Xslate to provide a parser for Handlebars templates. In most ways, this module functions identically to Text::Xslate, except that it parses Handlebars templates instead.
Text::Handlebars accepts an additional constructor parameter of helpers to define Handlebars-style helper functions. Standard helpers are identical to functions defined with the function parameter, except that they receive the current context implicitly as the first parameter (since perl doesn't have an implicit this parameter). Block helpers also receive the context as the first parameter, and they also receive the options parameter as a hashref. As an example:
helpers
function
this
options
sub { my ($context, $items, $options) = @_; my $out = "<ul>"; for my $item (@$items) { $out .= "<li>" . $options->{fn}->($item) . "</li>"; } return $out . "</ul>\n"; },
defines a simple block helper to generate a <ul> list.
<ul>
Text::Handlebars also overrides render and render_string to allow using any type of data (not just hashrefs) as a context (so rendering a template consisting of only {{.}} works properly).
render
render_string
{{.}}
The auto-indenting behavior for partials is not yet implemented, due to limitations in Text::Xslate.
The data parameter for @foo variables when calling $options->{fn}->() is not supported, because I don't understand its purpose. If someone wants this functionality, feel free to let me know, and tell me why.
data
@foo
$options->{fn}->()
Please report any bugs through RT: email bug-text-handlebars at rt.cpan.org, or browse to http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-Handlebars.
bug-text-handlebars at rt.cpan.org
http://handlebarsjs.com/
Text::Xslate
You can find this documentation for this module with the perldoc command.
perldoc Text::Handlebars
You can also look for information at:
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Text-Handlebars
CPAN Ratings
http://cpanratings.perl.org/d/Text-Handlebars
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Text-Handlebars
Search CPAN
http://search.cpan.org/dist/Text-Handlebars
Jesse Luehrs <doy at cpan dot org>
This software is Copyright (c) 2013 by Jesse Luehrs.
This is free software, licensed under:
The MIT (X11) License
To install Text::Handlebars, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Text::Handlebars
CPAN shell
perl -MCPAN -e shell install Text::Handlebars
For more information on module installation, please visit the detailed CPAN module installation guide.