9

I want to use fancy postfix dereferencing in my Mojo templates. I suppose I could do

% use experimental 'postderef';

at the top of every template file, but that seems repetitive and lame. Is there a way I can make Mojolicious import my pragma preferences to the lexical scope of every template?

friedo
  • 65,762
  • 16
  • 114
  • 184

2 Answers2

5

You can reload EPRenderer plugin with own options (default is without options), option template contains default values for Mojo::Template.

use Mojolicious::Lite;

plugin 'EPRenderer', template => { prepend  => 'use experimental "postderef";use Data::Dump "pp";'};

get '/' => sub { shift->render('index'); };

app->start;
__DATA__

@@ index.html.ep
% layout 'default';
% title 'Welcome';

Welcome to the Mojolicious real-time web framework!

% my $a = [[0]];
% push $a->[0]->@*, 1;
%=  pp($a)

@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
  <head><title><%= title %></title></head>
  <body><%= content %>

  </body>
</html>
vitas
  • 598
  • 5
  • 9
0

If you use that pragma in your Mojolicious App, it should work for the templates as well.

If not, then you could add it to a layout and use that layout from your templates.

arafeandur
  • 196
  • 1
  • 5
  • 1
    Adding the pragma to the app does not affect the templates (since they're compiled in a different scope.) But adding it to the layout is an idea I haven't tried. I'll give it a shot. – friedo Jul 25 '14 at 20:00
  • Sadly it looks like adding the pragma to the layout has no effect on the templates that use it. – friedo Jul 25 '14 at 20:06