Last active
August 29, 2015 14:04
-
-
Save thowe/21ecffa08c203248a40e to your computer and use it in GitHub Desktop.
Can't call a block within a block, how to recurse? Updated with working example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
# Documentation browser under "/perldoc" | |
#plugin 'PODRenderer'; | |
get '/' => sub { | |
my $c = shift; | |
my $thing = [ { 'name' => 'Parent', | |
'children' => | |
[ { 'name' => 'Child 1', | |
'children' => | |
[ { 'name' => 'Grandchild 1' }, | |
{ 'name' => 'Grandchild 2' } | |
] | |
}, | |
{ 'name' => 'Child 2'} | |
] | |
} | |
]; | |
$c->stash('family' => $thing); | |
$c->render('index'); | |
}; | |
app->start; | |
__DATA__ | |
@@ index.html.ep | |
% layout 'default'; | |
% title 'Welcome'; | |
% my $familyblock = begin | |
% my ($familyblock, $children) = @_; | |
<ul> | |
% foreach my $child (@$children) { | |
<li>Family member name is: <%= $child->{'name'} %></li> | |
% if ( $child->{'children'} ) { | |
%= $familyblock->($familyblock, $child->{'children'}); | |
% } | |
% } | |
</ul> | |
% end | |
<%= $familyblock->($familyblock, $family) %> | |
@@ layouts/default.html.ep | |
<!DOCTYPE html> | |
<html> | |
<head><title><%= title %></title></head> | |
<body><%= content %></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment