Skip to content

Instantly share code, notes, and snippets.

@pokutuna
Last active January 6, 2017 03:42
Show Gist options
  • Save pokutuna/a5c74714cfbe245c891df11234d0e1c6 to your computer and use it in GitHub Desktop.
Save pokutuna/a5c74714cfbe245c891df11234d0e1c6 to your computer and use it in GitHub Desktop.
😢 Text::Xslate::Util::hash_with_default
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Section::Simple;
use Text::Xslate::Util;
use Text::Xslate;
my $vpath = Data::Section::Simple->new()->get_data_section();
my $vars = { foo => 'hoge', bar => 'fuga' };
my $xslate = Text::Xslate->new(path => [$vpath]);
# OK
warn $xslate->render('base.tx', $vars); #=> foo:hoge, bar:fuga
# NG: missing foo
my $tied = Text::Xslate::Util::hash_with_default($vars, sub { "FILL @_" });
warn $xslate->render('base.tx', $tied); #=> foo:, bar:fuga
# OK with PP
require Text::Xslate::PP;
my $xslate_pp = Text::Xslate->new(path => [$vpath]);
warn $xslate_pp->render('base.tx', $tied); #=> foo:hoge, bar:fuga
__DATA__
@@ base.tx
:include _partial { foo => $foo }
@@ _partial.tx
foo:<: $foo :>, bar:<: $bar :>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment