Search code examples
perlmojolicious

How to use new syntax features in Mojolicious templates


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?


Solution

  • 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>