Mango fun with Catalyst

jk2addict on 2007-07-06T17:55:18

Now that the pet crisis is under control, the holiday has passed and I'm done tinkering with my new Ubuntu install, time to get back to work on Mango.

One of the things I've always hated bout Catalyst applications is that most times, 'make install' either turned the application into crap, or customizing the app is damn near impossible without some form of local svn/svk merge madness. It always feels that most Cat apps are single use, non reusable piles of code. Take it or leave it, as it shipped. That's not good for a Cat/Handel/DBIC ecom solution, where people still need to customize it and make it theirs.

From the onset, I'd tried to approach Mango as a Catalyst+Handel framework, not an actual application. Sure, it will ship with a working site..just run n go. But that's not it's focus [ironically enough]. Instead, Mango is a bunch of parts that people will want to customize, while at the same time being able to 'make install' upgrade without having to merge changes.

After a pile of tinkering, I've finally come upon a solution that works fairly well for me.

As an example, let's look at the code to admin users. We'd have a core domain model, a cat model, a cat controller, tt templates, and fb forms. Most of which could be overridden by the user if need be.

DOMAIN MODEL Mango::Provider::Users/Mango::User does the heavy lifting of user CRUD.

CATALYST MODEL: Mango::Catalyst::Model::Users is just wrapper around the domain model that knows how to get config from Catalyst.

MYAPP MODEL: MyApp::Model::Users is just a wrapper around Mango::Catalyst::Model::Users

CATALYST CONTROLLER Mango::Catalyst::Controller::Admin::Users has the usual chained load/edit/create/index type of things.

MYAPP CONTROLLER MyApp::Controller::Admin::Users is just wrapper around Mango::Catalyst::Controller::Admin::Users

CATALYST VIEW Mango::Catalyst::View::HTML is just a TT view with logic to use $sharedir, then local root templates.

MYAPP VIEW MyApp::View::HTML is just a wrapper around Mango::Catalyst::View::HTML.

TT TEMPLATES The default templates get installed with Mango, and are located in the distributions 'auto' shared directory (File::ShareDir).

FORMS The default forms get installed with Mango, and are located in the distributions 'auto' shared directory (File::ShareDir).

Since everything in MyApp is paper thin, upgrading Mango by doing 'make install' just works. No merging.

CUSTOMIZING THE MODEL Want to use a different Model? make your Users model use something else, even a different Mango::Provider.

CUSTOMIZING THE CONTROLLER Want to change the url from /admin/users to /administration/users? Just rename your controller. Hate how load() is implemented? override it. Want to use different forms? Set :FormFile(). Want to look for templates in a different path? Set template_paths().

CUSTOMIZING VIEW Use something other than tt...and your own templates.

CUSTOMIZING TEMPLATES This is the fun one. The Mango views are setup to look in $sharedir first, then in root/templates.

So, want to tweak the includes/pager template? put it in root/templates/includes/page. All of the other templates are still pulled from $sharedir. The gets even more entertaining for the XHTML vs. HTML view. The XHTML view uses the HTML templates by default (just served with the correct content-type). You can of course, provide local templates that to more xhtml specific things for the XHTML view to find.

Yada yada, etc, etc. In the end, I have something that is customizable at almost every point that can also be upgraded without users worrying about having their local changes overwritten...but it wasn't easy, and it certainly strays from the learning-catalyst tutorials.

In the end, when it's all working, I'd like to do a tutorial on how to do some of the template jockeying using ShareDir and even see some of that magic end up in Cat itself.