#!/usr/bin/env perl use strict; use warnings; use lib 't/lib'; use Test::More; use Test::Handlebars; render_ok( { helpers => { noop => sub { my ($context, $options) = @_; return $options->{fn}->($context); }, }, }, <<'TEMPLATE',

{{title}}

{{#noop}}{{body}}{{/noop}}
TEMPLATE { title => 'A', body => 'the first letter' }, <<'RENDERED',

A

the first letter
RENDERED "noop helper" ); render_ok( { helpers => { with => sub { my ($context, $new_context, $options) = @_; return $options->{fn}->($new_context); }, }, }, <<'TEMPLATE',

{{title}}

{{#with story}}
{{{intro}}}
{{{body}}}
{{/with}}
TEMPLATE { title => 'First Post', story => { intro => 'Before the jump', body => 'After the jump', }, }, <<'RENDERED',

First Post

Before the jump
After the jump
RENDERED "with helper" ); render_ok( { helpers => { with => sub { my ($context, $new_context, $options) = @_; return $options->{fn}->($new_context); }, each => sub { my ($context, $list, $options) = @_; my $ret = ''; for my $new_context (@$list) { $ret .= $options->{fn}->($new_context); } return $ret; }, }, }, <<'TEMPLATE',

{{title}}

{{#with story}}
{{{intro}}}
{{{body}}}
{{/with}}
{{#each comments}}

{{subject}}

{{{body}}}
{{/each}}
TEMPLATE { title => 'First Post', story => { intro => 'Before the jump', body => 'After the jump', }, comments => [ { subject => "Subject A", body => "Body A" }, { subject => "Subject B", body => "Body B" }, ], }, <<'RENDERED',

First Post

Before the jump
After the jump

Subject A

Body A

Subject B

Body B
RENDERED "each helper" ); render_ok( { helpers => { list => sub { my ($context, $items, $options) = @_; my $out = "\n"; }, }, }, <<'TEMPLATE', {{#list nav}} {{title}} {{/list}} TEMPLATE { nav => [ { url => 'http://www.yehudakatz.com', title => 'Katz Got Your Tongue', }, { url => 'http://www.sproutcore.com/block', title => 'SproutCore Blog', }, ], }, <<'RENDERED', RENDERED "list helper" ); render_ok( { helpers => { if => sub { my ($context, $conditional, $options) = @_; if ($conditional) { return $options->{fn}->($context); } return ''; }, }, }, <<'TEMPLATE', {{#if isActive}} Active {{/if}} TEMPLATE { isActive => 1, }, <<'RENDERED', Active RENDERED "if helper (true)" ); render_ok( { helpers => { if => sub { my ($context, $conditional, $options) = @_; if ($conditional) { return $options->{fn}->($context); } return ''; }, }, }, <<'TEMPLATE', {{#if isActive}} Active {{/if}} TEMPLATE { isActive => 0, }, <<'RENDERED', RENDERED "if helper (false)" ); render_ok( { helpers => { if => sub { my ($context, $conditional, $options) = @_; if ($conditional) { return $options->{fn}->($context); } else { return $options->{inverse}->($context); } }, }, }, <<'TEMPLATE', {{#if isActive}} Active {{else}} Inactive {{/if}} TEMPLATE { isActive => 1, }, <<'RENDERED', Active RENDERED "if/else helper (true)" ); render_ok( { helpers => { if => sub { my ($context, $conditional, $options) = @_; if ($conditional) { return $options->{fn}->($context); } else { return $options->{inverse}->($context); } }, }, }, <<'TEMPLATE', {{#if isActive}} Active {{else}} Inactive {{/if}} TEMPLATE { isActive => 0, }, <<'RENDERED', Inactive RENDERED "if/else helper (false)" ); render_ok( { helpers => { list => sub { my ($context, $items, $options) = @_; my $attrs = join ' ', map { "$_=\"$options->{hash}{$_}\"" } sort keys %{ $options->{hash} }; return "\n"; }, }, }, <<'TEMPLATE', {{#list nav id="nav-bar" class="top"}} {{title}} {{/list}} TEMPLATE { nav => [ { url => 'http://www.yehudakatz.com', title => 'Katz Got Your Tongue', }, { url => 'http://www.sproutcore.com/block', title => 'SproutCore Blog', }, ], }, <<'RENDERED', RENDERED "helper arguments" ); { local $TODO = "unimplemented"; local $SIG{__WARN__} = sub { }; # XXX this is almost certainly not what the api should be like, but i don't # understand the purpose for this feature well enough to come up with anything # more reasonable. feedback welcome! render_ok( { helpers => { list => sub { my ($context, $items, $options) = @_; my $out = ""; return $out; }, }, }, <<'TEMPLATE', {{#list array}} {{@index}}. {{title}} {{/list}} TEMPLATE { array => [ "Foo", "Bar", "Baz", ], }, <<'RENDERED', RENDERED "helper private variables" ); } render_ok( { helpers => { list => sub { my ($context, $items, $options) = @_; if (@$items) { my $out = ''; return $out; } else { return '

' . $options->{inverse}->($context) . '

'; } }, }, }, q[{{#list people}}{{name}}{{^}}Nobody's here{{/list}}], { people => [ { name => 'Alan' }, { name => 'Yehuda' }, ], }, q[], "helper with inverse" ); render_ok( { helpers => { list => sub { my ($context, $items, $options) = @_; if (@$items) { my $out = ''; return $out; } else { return '

' . $options->{inverse}->($context) . '

'; } }, }, }, q[{{#list people}}{{name}}{{^}}Nobody's here{{/list}}], { people => [], }, q[

Nobody's here

], "helper with inverse (empty)" ); render_ok( { helpers => { list => sub { my ($context, $items, $options) = @_; if (@$items) { my $out = ''; return $out; } else { return '

' . $options->{inverse}->($context) . '

'; } }, }, }, q[{{#list people}}Hello{{^}}{{message}}{{/list}}], { people => [], message => "Nobody's here", }, q[

Nobody's here

], "helper with inverse (inverse has variables)" ); done_testing;