The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Text::Handlebars - http://handlebarsjs.com/ for Text::Xslate

VERSION

version 0.04

SYNOPSIS

  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>

DESCRIPTION

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:

  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.

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).

BUGS/CAVEATS

  • 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.

Please report any bugs to GitHub Issues at https://github.com/doy/text-handlebars/issues.

SEE ALSO

http://handlebarsjs.com/

Text::Xslate

SUPPORT

You can find this documentation for this module with the perldoc command.

    perldoc Text::Handlebars

You can also look for information at:

AUTHOR

Jesse Luehrs <doy@tozt.net>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2014 by Jesse Luehrs.

This is free software, licensed under:

  The MIT (X11) License