I don't make any attempts to hide my (occasional) disregard for "best practices" or logical clarity in code. I even admitted just how much I do so in a recent thread. In this post I want to present an example of something I did in an application I am writing for a paying client and ask the wiser, more orthodox monks to either explain how (and why) to make my methodology a little more kosher, or perhaps it really is an acceptable WTDI for my purposes.
At the bottom of this explanation is a Catalyst method to load CSS and JS files for my main template. The template wrapper has a section like this:
[% FOREACH file IN css %]<link rel="stylesheet" href='[%c.uri_for("/ro
+ot/static/css/$file.css")%]' />[%END%]
[% FOREACH file IN js %]<script src='[%c.uri_for("/root/static/js/$fil
+e.js")%]'></script>[%END%]
Each action would then push the files that needed to be included into arrayrefs in the stash. This worked at first but was somewhat cumbersome, and required me to remember the names of each file and which were needed for what functionality:
sub register :Local {
...
push @{$c->stash->{js}}, qw/
jquery-1.7.2.min
jquery-ui-1.8.20-custom.min
jquery.form
jquery.validate
phone-validate
/;
push @{$c->stash->{css}}, qw|
ui-theme/style
forms
|;
}
This quickly became a pain in the ass, but I didn't write a "solution" until this morning. I wanted to reduce the above a single statement like so:
$c->forward('include',[qw/jq ui form/]);. I realized that the hash of include definitions should go either into the global YAML configuration file (if I used a YAML config file), or at least with the rest of the application configurations (in the main module for the package), but I don't know enough about how Catalyst works to be able to retrieve this configuration from within my Controller. I also considered that, since this is all template stuff, I should really write this into my config template file (I do have one) and then rewrite my wrapper template logic to pull things fro there, but I really didn't want to figure out how to do that, so I just made an anonymous hash inside the
foreach loop that processes the arguments for the
include action and passed each argument to it to get the needed files. Actually the anonymous hash is inside the assignment of another hash, which I'm sure adds to the computational complexity, processing time, references floating around behind the scenes, the same thing getting instantiated over and over for no reason, etc. But it makes
perfect sense to me.
sub include :Private {
my ($self, $c, @include) = @_
foreach (@include) {
my %links = (
css => {
ui => ["aristo/theme"],
bracket => [qw/ bracket match /],
datetime => ["datetimepicker"],
table => ["themes/blue/style"],
}->{$_},
js => {
jq => ["jquery-1.7.2.min"],
ui => ["jquery-ui-1.8.20.custom.min"],
form => [qw/jquery.form jquery.validate.min phone-vali
+dation/],
table => ["jquery.tablesorter.min"],
datetime => [qw/ jquery-ui-timepicker-addon jquery-ui-
+slider-access/],
bracket => ["bracket"],
swap => ['jquery.swap'],
}->{$_}
);
push @{$c->stash->{css}}, $_ foreach (@{$links{css}});
push @{$c->stash->{js}}, $_ foreach (@{$links{js}});
}
}