#!/usr/bin/env perl use strict; use warnings; use lib 't/lib'; use Test::More; use Test::Handlebars; render_ok( { function => { 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( { function => { 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( { function => { 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( { function => { 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( { function => { 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( { function => { 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)" ); { local $TODO = "unimplemented"; local $SIG{__WARN__} = sub { }; render_ok( { function => { 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( { function => { 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( { function => { list => sub { my ($context, $items, $options) = @_; my $attrs = join ' ', map { $_ => $options->{hash}{$_} } sort keys %{ $options->{hash} }; return ""; }, }, }, <<'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" ); render_ok( { function => { 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" ); } done_testing;