How can I use Mojolicious rendering in a standalone Perl script?

I want to display .html.ep templates using the Mojolicious engine engine in a stand-alone script that sends emails and runs from cron:

#!/usr/bin/perl

use feature ':5.10';

use Mojo::Base -strict;
use Mojolicious::Renderer;
use Data::Dumper;


my $renderer = Mojolicious::Renderer->new();
push @{$renderer->paths}, '/app/templates';

my $template = $renderer->get_data_template({
    template => 'template_name',
    format => 'html',
    handler => 'ep'
});

print Dumper($template) . "\n";

However, it is $templatealways undefined.

Template file /app/templates/template_name.html.ep.

What am I doing wrong?

+4
source share
1 answer

You are using get_data_templatefrom Mojo :: Renderer, which is used to load templates from the section of __DATA__your current source code file.

, Mojo:: Renderer - . Mojo::Template, .

use Mojo::Template;

my $mt = Mojo::Template->new( vars => 1 );
my $email_body = $mt->render_file( 'test.html.ep', { one => 1, two => 2 } );
say $email_body;

test.html.ep:

The magic numbers are <%= $one %> and <%= $two %>.

:

The magic numbers are 1 and 2.

vars , .

+8

Source: https://habr.com/ru/post/1669186/


All Articles