Skip to content

Instantly share code, notes, and snippets.

@suzak
Created August 1, 2011 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suzak/1118268 to your computer and use it in GitHub Desktop.
Save suzak/1118268 to your computer and use it in GitHub Desktop.
Text::Xslate: Oops: Refers to unallocated local variable 0 (> -1)
#!perl
use strict;
use warnings;
use Data::Section::Simple;
use List::Util ();
use Text::Xslate;
my $tx = Text::Xslate->new(
path => Data::Section::Simple->new->get_data_section,
cache_dir => '/tmp/xslate',
function => {
array => sub {
return List::Util::reduce {
return $a unless $b;
push @$a, ref $b && ref $b eq 'ARRAY' ? @$b : $b;
$a;
} [], @_;
},
is_array => sub {
my ($obj) = @_;
return ref $obj && ref $obj eq 'ARRAY';
},
},
);
print $tx->render('index.html', {
c => C->new,
});
package C;
use strict;
use warnings;
sub new {
my ($class) = @_;
return bless { }, $class;
}
sub uri_for {
my ($self, $path) = @_;
return 'http://example.com' . $path;
}
sub in_production { 1 }
package main;
__DATA__
@@ _tx/macros.tx
:# common macros
: macro css_tag -> $css {
<link rel="stylesheet" href="<: $css :>" />
: }
: macro script_tag -> $js {
: if is_array($js) {
<script type="text/javascript" src="<: $js.0 :>"<: for $js.1.kv() -> $p { :> <: $p.key :><: if defined $p.value { :>="<: $p.value :>"<: } } :>></script>
: } else {
<script type="text/javascript" src="<: $js :>"></script>
: }
: }
@@ _tx/wrapper/base.tx
: cascade with _tx::macros
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title><: block title { :>title<: } :></title>
: for $css -> $i { css_tag($i) }
: for $js -> $i { script_tag($i) }
</head>
<body>
: block body { }
</body>
</html>
@@ _tx/wrapper.tx
: my $jquery = 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery' ~ ($c.in_production ? '.min' : '') ~ '.js';
: cascade _tx::wrapper::base {
:# js => [$jquery, [$c.uri_for('/js/site.js'), { charset => 'utf-8' }], @$js],
: js => array(
: $jquery,
: [[$c.uri_for('/js/site.js'), { charset => 'utf-8' }]],
: $js),
:# css => [$c.uri_for('/css/site.css'), @$css],
: css => array(
: $c.uri_for('/css/site.css'), $css),
: }
: around body {
<header><h1>title</h1></header>
: block content { }
: }
@@ index.html
: cascade _tx::wrapper {
: js => [ ],
: css => [ ],
: }
: around content {
content
: }
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>title</title>
<link rel="stylesheet" href="http://example.com/css/site.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="http://example.com/js/site.js" charset="utf-8"></script>
</head>
<body>
<header><h1>title</h1></header>
content
</body>
</html>
$ perl-5.14.1 app.pl
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>title</title>
<link rel="stylesheet" href="http://example.com/css/site.css" />
<script type="text/javascript" src="http://example.com/js/site.js" charset="utf-8"></script>
</head>
<body>
<header><h1>title</h1></header>
content
</body>
</html>
$ perl-5.8.9 app.pl
Text::Xslate: Oops: Refers to unallocated local variable 0 (> -1) at app.pl line 27.
(index.html:5) at app.pl line 27
#!perl
use strict;
use warnings;
use Data::Section::Simple;
use Test::Exception;
use Test::More;
use Text::Xslate;
my $tx = Text::Xslate->new(
cache => 0,
path => Data::Section::Simple->new->get_data_section,
function => {
t => sub { 1 },
},
);
for (qw(ok1 ok2 ok3 ng)) {
my $rendered;
lives_ok { $rendered = $tx->render("$_.html") };
is $rendered, 'ok';
}
done_testing;
__DATA__
@@ wrapper.tx
: $ok
@@ ok1.html
: cascade wrapper { ok => (t ? 'ok' : 'ng') }
@@ ok2.html
: my $ok = 'ok';
: cascade wrapper { ok => $ok }
@@ ok3.html
: my $ok = (true ? 'ok' : 'ng');
: cascade wrapper { ok => $ok }
@@ ng.html
: my $ok = (t ? 'ok' : 'ng') ;
: cascade wrapper { ok => $ok }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment