global stash variables and functions?

866 views
Skip to first unread message

iaw4

unread,
Jan 10, 2012, 9:07:48 PM1/10/12
to Mojolicious

dear mojo-experts---total newbie here. mojolicious is an amazing
revelation for anyone like me who has hand-coded cgi apps before...but
the learning curve is non-trivial, and the docs are still incomplete.
I hope some of you experts will write a book on its use that I can
purchase.

simple question: Is there something like a global stash? I have two
uses that I am thinking of. One is to display the count of all
registered users to every user. The other is to display the current
user name. so, in 'default.html.ep', I want to put a line like

<p>Hello user '<=% $self->user()||"noone yet" %>'.
Our system has currently <=% globalcount() %> registered users.</p>

right now, each of my functions adds self->stash(uid => $self->user(),
globalcount => globalcount(), ...). not pretty.

is this easy?

sincerely,

/iaw

Виктор Турский

unread,
Jan 11, 2012, 2:08:05 AM1/11/12
to mojol...@googlegroups.com
2012/1/11 iaw4 <ivo...@gmail.com>:

> right now, each of my functions adds self->stash(uid => $self->user(),
> globalcount => globalcount(), ...).  not pretty.

You can use bridge in your routes:
my $r = $self->routes->bridge->to('sessions#fill_stash');

and then create routes as usual
$r->get('/page')->to('controller#action');


--
Турский Виктор
http://koorchik.blogspot.com

ivo welch

unread,
Jan 11, 2012, 11:13:58 AM1/11/12
to mojol...@googlegroups.com

hi BNKTOP(?):  could you please be a little more explicit with an example?

best,

/iaw

----
Ivo Welch (ivo....@gmail.com)
http://www.ivo-welch.info/
J. Fred Weston Professor of Finance
Anderson School at UCLA, C519




--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To post to this group, send email to mojol...@googlegroups.com.
To unsubscribe from this group, send email to mojolicious...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mojolicious?hl=en.


Виктор Турский

unread,
Jan 11, 2012, 1:28:08 PM1/11/12
to mojol...@googlegroups.com
2012/1/11 ivo welch <ivo....@gmail.com>:

>
> hi BNKTOP(?):  could you please be a little more explicit with an example?

For example, you have 3 routes and 3 actions

sub startup {
my $self = shift;
my $r = $self->routes;
$r->get('/user_posts')->to('user#show_posts');
$r->get('/user_messages')->to('user#show_messages');
$r->get('/user_files')->to('user#show_files');
}

for instance, in your layout you use something like this:

Global count: <%= $globalcount %>
User id: <%= $user_id %>

So, you should in every action will stash with globalcount and user_id values
$self->stash(user_id => $user_id, globalcount => $globalcount);

But this is inconvinient.

To solve this you can use bridge in next way:

sub startup {
my $self = shift;
my $r = $self->routes;
my $br = $r->bridge->to('user#fill_stash');

$br->get('/user_posts')->to('user#show_posts');
$br->get('/user_messages')->to('user#show_messages');
$br->get('/user_files')->to('user#show_files');
}

package User;
sub fill_stash {
my $self = shift;
$self->stash(user_id => $user_id, globalcount => $globalcount);
return 1;
}

...

So, fill_stash will be called before any of your actions. This allows
you to fill stash in one place.

See Mojolicious::Guides::Routing for details.

--
Turskyi Viktor
http://koorchik.blogspot.com

John Stoffel

unread,
Jan 12, 2012, 3:20:27 PM1/12/12
to mojol...@googlegroups.com
>>>>> "Виктор" == Виктор Турский <koor...@gmail.com> writes:

Виктор> 2012/1/11 ivo welch <ivo....@gmail.com>:


>>
>> hi BNKTOP(?):  could you please be a little more explicit with an example?

Виктор> For example, you have 3 routes and 3 actions

Thank you so much for this example, it's short, clear and to the
point. And makes alot of sense on *why* you would use this.


Виктор> sub startup {
Виктор> my $self = shift;
Виктор> my $r = $self->routes;
Виктор> $r->get('/user_posts')->to('user#show_posts');
Виктор> $r->get('/user_messages')->to('user#show_messages');
Виктор> $r->get('/user_files')->to('user#show_files');
Виктор> }

Виктор> for instance, in your layout you use something like this:

Виктор> Global count: <%= $globalcount %>
Виктор> User id: <%= $user_id %>

Виктор> So, you should in every action will stash with globalcount and user_id values
Виктор> $self->stash(user_id => $user_id, globalcount => $globalcount);

Виктор> But this is inconvinient.

Виктор> To solve this you can use bridge in next way:

Виктор> sub startup {
Виктор> my $self = shift;
Виктор> my $r = $self->routes;
Виктор> my $br = $r->bridge->to('user#fill_stash');

Виктор> $br->get('/user_posts')->to('user#show_posts');
Виктор> $br->get('/user_messages')->to('user#show_messages');
Виктор> $br->get('/user_files')->to('user#show_files');
Виктор> }

Виктор> package User;
Виктор> sub fill_stash {
Виктор> my $self = shift;
Виктор> $self->stash(user_id => $user_id, globalcount => $globalcount);
Виктор> return 1;
Виктор> }

Виктор> ...

Виктор> So, fill_stash will be called before any of your actions. This allows
Виктор> you to fill stash in one place.

Виктор> See Mojolicious::Guides::Routing for details.

Виктор> --
Виктор> Turskyi Viktor
Виктор> http://koorchik.blogspot.com

Виктор> --
Виктор> You received this message because you are subscribed to the Google Groups "Mojolicious" group.
Виктор> To post to this group, send email to mojol...@googlegroups.com.
Виктор> To unsubscribe from this group, send email to mojolicious...@googlegroups.com.
Виктор> For more options, visit this group at http://groups.google.com/group/mojolicious?hl=en.

--

Reply all
Reply to author
Forward
0 new messages