A Killer Feature for Mojolicious

I just thought of a truly killer feature for Mojolicious. You are undoubtedly asking yourself why I don't just share this with the Mojolicious team on IRC or GitHub, etc. Most of you undoubtedly already know the answer to that, but I digress.

Many shops that I've worked with have this concept generally referred to as content-injection or template filtering depending on how you go about it. It is basically a means of hooking into template rendering after the template has been rendered but before it has been output (or otherwise used by the application).

The problem this solves is that some things are best left to be handled by the template, while other things are best done by using regex or a DOM parser.

I believe Mojolicious is at a unique advantage to offer something like this out-of-the-box because it has it's own templating system -and- it's own DOM parser. An example of a mojolicious template using the technique I'm describing could be as follows:

__DATA__

@@ alter_me.html.ep
<!DOCTYPE html>
<html>
  <head>
    <title>Alter me!</title>
  </head>
  <body>You've been altered.</body>
</html>

% filter begin
% my ($c, $dom) = @_; # is passed the DOM parser using rendered template
  $dom->at('title')->replace_content('because I can');
% end

@@ further_convince_me.html.ep
<!DOCTYPE html>
<html>
  <head>
    <title>Convince me!</title>
  </head>
  <body>
    <select id="industry" value="" name="industry">
      <option id="industry" value="" name="industry">Please Select…</option>
      <option id="industry" value="Aerospace &amp; Defense" name="industry">Aerospace &amp; Defense</option>
      <option id="industry" value="Agriculture" name="industry">Agriculture</option>
      <option id="industry" value="Automotive &amp; Transport" name="industry">Automotive &amp; Transport</option>
      <option id="industry" value="Banking" name="industry">Banking</option>
      <option id="industry" value="Beverages" name="industry">Beverages</option>
      <option id="industry" value="Business Services" name="industry">Business Services</option>
      <option id="industry" value="Charitable Organizations" name="industry">Charitable Organizations</option>
      <option id="industry" value="Chemicals" name="industry">Chemicals</option>
      <option id="industry" value="Computer Hardware" name="industry">Computer Hardware</option>
      <option id="industry" value="Computer Services" name="industry">Computer Services</option>
      <option id="industry" value="Computer Software" name="industry">Computer Software</option>
      <option id="industry" value="Construction" name="industry">Construction</option>
      <option id="industry" value="Consumer Products Manufacturers" name="industry">Consumer Products Manufacturers</option>
      <option id="industry" value="Consumer Services" name="industry">Consumer Services</option>
      <option id="industry" value="Cultural Institutions" name="industry">Cultural Institutions</option>
      <option id="industry" value="Education" name="industry">Education</option>
      <option id="industry" value="Electronics" name="industry">Electronics</option>
      <option id="industry" value="Energy &amp; Utilities" name="industry">Energy &amp; Utilities</option>
      <option id="industry" value="Entertainment" name="industry">Entertainment</option>
      <option id="industry" value="Environmental Services &amp; Equipment" name="industry">Environmental Services &amp; Equipment</option>
      <option id="industry" value="Financial Services" name="industry">Financial Services</option>
      <option id="industry" value="Food" name="industry">Food</option>
      <option id="industry" value="Foundations" name="industry">Foundations</option>
      <option id="industry" value="Government" name="industry">Government</option>
      <option id="industry" value="Health Care" name="industry">Health Care</option>
      <option id="industry" value="Industrial Manufacturing" name="industry">Industrial Manufacturing</option>
      <option id="industry" value="Insurance" name="industry">Insurance</option>
      <option id="industry" value="Leisure" name="industry">Leisure</option>
      <option id="industry" value="Media" name="industry">Media</option>
      <option id="industry" value="Membership Organizations" name="industry">Membership Organizations</option>
      <option id="industry" value="Metals &amp; Mining" name="industry">Metals &amp; Mining</option>
      <option id="industry" value="Pharmaceuticals" name="industry">Pharmaceuticals</option>
      <option id="industry" value="Real Estate" name="industry">Real Estate</option>
      <option id="industry" value="Retail" name="industry">Retail</option>
      <option id="industry" value="Security Products &amp; Services" name="industry">Security Products &amp; Services</option>
      <option id="industry" value="Telecommunications Equipment" name="industry">Telecommunications Equipment</option>
      <option id="industry" value="Telecommunications Services" name="industry">Telecommunications Services</option>
      <option id="industry" value="Transportation Services" name="industry">Transportation Services</option>
      <option id="industry" value="Technology" name="industry">Technology</option>
      <option id="industry" value="Other" name="industry">Other</option>
  </select>
  </body>
</html>

% filter begin
% my ($c, $dom) = @_;
  $dom->at('#industry option[value*="'. $c->param('industry') .'"]')->attrs(selected => 'selected');
% end

Leave a comment

About Al Newkirk

user-pic ... proud Perl hacker, ask me anything!