Where to put view logic?

36 views
Skip to first unread message

James Szinger

unread,
Jul 29, 2020, 1:14:41 PM7/29/20
to mojol...@googlegroups.com, James Szinger
Hi,

I'm writing a fairly large Mojolicious app and I'd like some advice on
where to place the logic of the display or view. My model queries a
database and returns a data stucture with the results. Then my
templates pull the data from the structure and format it. I think
this is all fairly normal.

The problem is that some of the display logic is fairly complicated:
one of my templates has a 30 line <% %> block before anything can be
displayed. It strikes me that these 30 lines (and others) are better
off in a subroutine or method. But where? MyApp::View? How would I
call it from a template? None of the documentation I can find
addresses this. I appreciate any pointers or examples.

Thanks,
Jim

P.S. The templates look something like:
<%
my @foobar;
for my $foo ( @row->{foo}) {
my $foobar;
$foobar .= "$foo->{baz}: " if $foo->{baz};
$foobar .= $foo->{bar}->join( ', ');
push @foobar, $foobar;
}
my $foobars = join '; ', @foobar;
%>
% if ($foobars) {
<p>Foobar is <%= $foobars %>.<p>
%>

Sebastian Riedel

unread,
Jul 29, 2020, 1:17:05 PM7/29/20
to Mojolicious
The problem is that some of the display logic is fairly complicated:
one of my templates has a 30 line <% %> block before anything can be
displayed. It strikes me that these 30 lines (and others) are better
off in a subroutine or method. But where? MyApp::View? How would I
call it from a template? None of the documentation I can find
addresses this. I appreciate any pointers or examples.

Scott H

unread,
Jul 29, 2020, 2:10:39 PM7/29/20
to mojol...@googlegroups.com, James Szinger
I probably do it backwards, i dont use dbix::class just mojo::pg and like you my app is a big one. I think the Plugin route is a good way to do it but I didnt go that way. I'm using Mojo::Base for my model classes.

I made a models folder so lib/Models/mymodelclass.pm
my controllers handle the views and the requests and create new objects of my models (i know i should use helpers...), my models handle anything that requires a database connection or something related to the table or view. I pass in the session, db, and anything else needed when making the object. So for an employee listing page for example, if i was getting a list of employees but needed to manipulate them, i would run my query, alter the data, return it. my controller picks it up and shoots it to the template view.

My models aren't always a db connection though sometimes I need small tweaks and I'll just make a subroutine in that model because it's still related in a way. Is this bad blending my model with a non database subroutine probably but its related to the database table/controller/view so i'm going to keep it how it is.

Hope this gives an idea.

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/20200729111238.1f86ccdc%40doliolum.thaliacea.lan.

James Szinger

unread,
Jul 29, 2020, 3:34:19 PM7/29/20
to Sebastian Riedel, mojol...@googlegroups.com
Thanks. The cookbook is especially helpful. I just didn't grasp its
significance the previous times I read it. I'll be creating
MyApp::Plugins::ViewHelpers.

Jim
Reply all
Reply to author
Forward
0 new messages