I'm fairly new to Mojolicious. I've tried researching this in docs, StackOverflow, and online examples. I haven't found an adequate answer, yet. Hopefully someone can help me.
I have a Controller method/action that is supposed to display a
User's profile and Account data. From within the method, I pull the
User and Account objects from the DB, and push them into stash so they
can be accessed by the template. I have used Dumper to ensure that they exist in stash pre-render time.
Controller action in MyApp::User
sub show_public_profile { my $self = shift; return $self->render_not_found if ( ! defined $self->stash('username') ); my $user = MyApp::User->new( username => $self->stash('username') ); my $loaded = $user->load( speculative => 1 ); if ( defined $user && $loaded != 0 ) { my $account = $user->account; $self->stash( user => $user, account => $account ); $LOGGER->debug( 'pre-template stash: '. Dumper($self->stash) ); return $self->render; } else { return $self->render_not_found; } }
Once I hit the template, however, nothing I've populated to the stash still exists.
Placeholder/test template: user/show_public_profile.html.ep
% my $user = stash 'user'; % my $account = stash 'account'; <pre>stash = <%= dumper stash %></pre> <pre>$user = <%= dumper $user %></pre> <pre>$account = <%= dumper $account %></pre> <h1>Profile for <%= $user->{'username'} %></h1>
Unfortunately, the inserted 'user' and 'account' values don't exist in the template's version of stash:
stash = { 'action' => 'show_public_profile', 'controller' => 'user', 'mojo.captures' => { 'action' => 'show_public_profile', 'controller' => 'user', 'username' => 'test_user' }, 'mojo.routed' => 1, 'mojo.started' => [ 1384869014, 663017 ], 'username' => 'test_user' }
Any clue as to what I'm doing wrong, here? I'm using full Mojolicious, not Mojolicious::Lite, which most examples reference. And, I've tried every permutation of example code I can find. What am I missing?
Thanks!
Regards,
-- Jason
Once I hit the template, however, nothing I've populated to the stash still exists.
Have you tried just using the variables directly in the template, i.e. NOT (re)setting them from an explicit call to stash as in the first 2 lines of your code snippet?
-- Code for inline template
package Mojo::Template::SandBox; use Mojo::Base -strict;sub { my $_M = ''; my $self = shift; my $_S = $self->stash; my $foo = $_S->{'foo'};; do { my $foo = stash "foo"; $_M .= _escape scalar $foo ;$_M .= "\n";; $_M } };
Have you tried just using the variables directly in the template, i.e. NOT (re)setting them from an explicit call to stash as in the first 2 lines of your code snippet?
Allan
--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/rx2Jy37xSIg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/rx2Jy37xSIg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/rx2Jy37xSIg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.
Global symbol "$user" requires explicit package name at template user/show_public_profile.html.ep line 5. Global symbol "$account" requires explicit package name at template user/show_public_profile.html.ep line 6. Global symbol "$user" requires explicit package name at template user/show_public_profile.html.ep line 7.
[Mon Nov 25 20:35:48 2013] [debug] Careful, "action" is a reserved stash value.
[Mon Nov 25 20:35:48 2013] [debug] Careful, "controller" is a reserved stash value.
Have you tried just using the variables directly in the template, i.e. NOT (re)setting them from an explicit call to stash as in the first 2 lines of your code snippet?
Allan
--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/rx2Jy37xSIg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.
[Mon Nov 25 20:35:48 2013] [debug] Careful, "action" is a reserved stash value.
[Mon Nov 25 20:35:48 2013] [debug] Careful, "controller" is a reserved stash value.
[badkarma@perlpoet ~/src/mojo_projects/MyApp]$ grep -r stash . | grep -v log
./lib/MyApp/User.pm: $self->stash( user => $user, account => $account );
./templates/user/show_public_profile.html.ep:<pre>stash = <%= dumper stash %></pre>
./templates/user/show_public_profile.html.ep:%# my $user = stash 'user';
./templates/user/show_public_profile.html.ep:%# my $account = stash 'account';
--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/rx2Jy37xSIg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.