Skip to content

Instantly share code, notes, and snippets.

@thowe
Last active August 29, 2015 14:04
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 thowe/21ecffa08c203248a40e to your computer and use it in GitHub Desktop.
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
#!/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