in reply to Perl code inside the Mojo::Template

First of all, I would very strongly recommend against using CGI.pm when you have Mojolicious available. Just use Mojo for everything! Update: I showed a code comparison between CGI.pm and Mojo in this node, and I have a bunch of Mojo examples on my scratchpad. /Update

% if($n->{'year'} eq <%= $number %> ) {

A line beginning with % is Perl code, and <%= $number %> is not valid Perl code. Try "% if($n->{'year'} eq $number ) {" instead.

ALso is it possible to use a sub routine from the Perl code inside the template?

Yes, you can use subroutines, though you usually need to import them into the template's namespace with a use statement or similar, by calling them by their full name (in your case main::name_val, though I personally find this a little ugly), you can change a template's namespace via the namespace option (though as the documentation says, be careful), or when using templates as part of a larger Mojo app, by installing them as a "helper" (see Mojolicious).

<td> % name_val(<%= $n->{'name'} %>) % </td>

Same problem here, try "<td><%= main::name_val( $n->{'name'} ) %></td>" instead.

Replies are listed 'Best First'.
Re^2: Perl code inside the Mojo::Template
by Anonymous Monk on Aug 06, 2021 at 19:07 UTC
    Thank you very much!
    I will work on getting rid of CGI.pm, need to get more into the way "Mojo::Template" deals with passing parameters from the Perl code to the template and vice-versa!