Mojolicious templates accessing stash values set by a Controller

534 views
Skip to first unread message

Jason Lamey

unread,
Nov 25, 2013, 10:22:34 AM11/25/13
to mojol...@googlegroups.com

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

sri

unread,
Nov 25, 2013, 10:56:45 AM11/25/13
to mojol...@googlegroups.com

Once I hit the template, however, nothing I've populated to the stash still exists.

There must be something else going on here that you're not showing us, because the example looks fine to me.

--
sebastian

Allan Cochrane

unread,
Nov 25, 2013, 12:11:26 PM11/25/13
to mojol...@googlegroups.com
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

sri

unread,
Nov 25, 2013, 12:32:18 PM11/25/13
to mojol...@googlegroups.com

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?

Shouldn't make a difference.

    $ MOJO_TEMPLATE_DEBUG=1 perl -Mojo -E 'a({foo => "bar", inline => q{<% my $foo = stash "foo"; %><%= $foo %>}})->start' get /

    -- 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 } };


    bar

--
sebastian

Jason Lamey

unread,
Nov 25, 2013, 1:40:04 PM11/25/13
to mojol...@googlegroups.com
Allan: Yeah, I have tried that.  Tells me the variables are global and needs to be scoped into a proper namespace (the same error you see if you don't use a 'my' in front of a local variable in Perl).


On Mon, Nov 25, 2013 at 12:11 PM, Allan Cochrane <allan.c...@gmail.com> wrote:
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.



--
Jason Lamey
--------------------------------------------
Infinite Monkeys Games
Owner / Game Designer

Jason Lamey

unread,
Nov 25, 2013, 1:41:39 PM11/25/13
to mojol...@googlegroups.com
Thanks for the reply, Sebastian.  I'm not sure what else to show you.  What other part of my codebase would be helpful to share in figuring out what I'm doing wrong?

-- Jason


--
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.

Allan Cochrane

unread,
Nov 25, 2013, 1:55:05 PM11/25/13
to mojol...@googlegroups.com
Hi,

so using the variables directly gives you an error message, yes? I don't think that should happen.

Maybe that is a clue that something else in your app is awry? Does SRI's example work for you?

Allan

Jason Lamey

unread,
Nov 25, 2013, 2:17:00 PM11/25/13
to mojol...@googlegroups.com
Allan,

Yeah, something is definitely off.  I'm just not sure what.  The codebase is still really small and young, so I'm not sure what's causing this issue.

Unfortunately, I'm at work, so I'll have to poke at SRI's example once I'm at home tonight. :(

-- Jason


--
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.

Jason Lamey

unread,
Nov 25, 2013, 8:45:25 PM11/25/13
to mojol...@googlegroups.com
Hi Allan (and SRI),

In trying your suggestion, I get the following error:

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.

I don't know if it means anything, but the following comes up, as well, when rendering templates, even though I'm not assigning anything explicitly to these reserved keys:

[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.

Thanks,
-- Jason


On 11/25/2013 12:11 PM, Allan Cochrane wrote:
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.

--
Jason Lamey | Freelancer Web Programming | Graphic Design | DJ/KJ

Sebastian Riedel

unread,
Nov 25, 2013, 8:52:08 PM11/25/13
to mojol...@googlegroups.com
[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.

These don't just show up out of nowhere, you certainly are doing something to cause them.

--

Jason Lamey

unread,
Nov 25, 2013, 9:18:54 PM11/25/13
to mojol...@googlegroups.com
I don't doubt it.  However, grepping from the root of my application, here's everything I do, so far, with stash:

[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';

So, I don't know what I'm doing wrong.  This is becoming very frustrating.

Regards,
-- Jason
--
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.
Reply all
Reply to author
Forward
0 new messages