Jesse Luehrs > Text-Handlebars-0.02 > Text::Handlebars

Download:
Text-Handlebars-0.02.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

Open  1
View/Report Bugs
Module Version: 0.02   Source   Latest Release: Text-Handlebars-0.04

NAME ^

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

VERSION ^

version 0.02

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 ^

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.

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 at cpan dot org>

COPYRIGHT AND LICENSE ^

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

This is free software, licensed under:

  The MIT (X11) License
syntax highlighting: