Skip to content

Instantly share code, notes, and snippets.

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 deserted/5b30d3c54e79692aaafc to your computer and use it in GitHub Desktop.
Save deserted/5b30d3c54e79692aaafc to your computer and use it in GitHub Desktop.
Trouble iterating
## Helper Definition
$app->helper('blog.getCategories' =>
sub {
my $self = shift;
my @cats = $self->db->resultset('BlogCategory')->all;
my @categories = map +{ id => $_->catid, name => $_->catname }, @cats; ## catid and catname both in DBIx::Class schema def
$self->app->log->debug(Dumper(\@categories));
return wantarray ? @categories : \@categories;
}
);
## Dumper returns
$VAR1 = [
{
'id' => '1',
'name' => 'news'
}
];
## shoved into stash then rendered
sub ajaxAddBlogPost {
my $self = shift;
$self->stash(postcats => $self->blog->getCategories);
$self->render(template => 'admin/page/createpost');
}
## TEmplate section (using template toolkit with Mojolicious::Plugin::TtRenderer)
<label>Category
<select name="postcategory">
[% FOREACH cat IN postcats %]
<option value="[% cat.id %]">[% cat.name %]</option>
[% END %]
</select>
</label>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment