Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

Trying to pass a variable value to the template from the Perl file:
From the .ep file:
<div> Number<%= $data->{ box } %> Test: <%= $test %> </div>

The Perl file:
... my $mt = Mojo::Template->new( vars => 1, auto_escape => 1 ); my $data = ... # Not where the issues is. my $code = "PERL"; print $q->header( -charset => 'utf-8' ), $mt->render_file( myfile.html +.ep, { data => $data }, { test => $code } ); ...

I am getting this error:
Global symbol "$test" requires explicit package name at myfile.html.ep

Cant get why, is there a better way of doing this?
Thanks for taking a look!

Replies are listed 'Best First'.
Re: Passing a value to Mojo::Template template
by haukex (Archbishop) on Apr 14, 2021 at 16:56 UTC

    The data for the variables should be in a single hash. $mt->render_file('myfile.html.ep', { data => $data, test => $code } ) works for me.

      Thats great and made me think about another possibility, if I had multiple values as:
      { data => $data, test => $code, color => 'blue', name => $names, ... }

      Could I combine all of it into one variable and than pass to "$mt->render_file()"?

      Thank you!
        Could I combine all of it into one variable and than pass to "$mt->render_file()"?

        I'm not sure I fully understand the question... do you mean something like the following (which works too)? If not, please provide more context, preferably in the form of an SSCCE (see also How do I post a question effectively?).

        my %vars = ( data => $data, test => $code, color => 'blue' ); print $mt->render_file( 'myfile.html.ep', \%vars );