How to use new syntax features in Mojolicious templates
Asked Answered
D

2

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?

Dactylic answered 20/7, 2014 at 23:12 Comment(0)
G
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>
Gobbet answered 3/10, 2014 at 16:3 Comment(0)
S
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.

Susumu answered 25/7, 2014 at 19:56 Comment(2)
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.Dactylic
Sadly it looks like adding the pragma to the layout has no effect on the templates that use it.Dactylic

© 2022 - 2024 — McMap. All rights reserved.