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.


In reply to Re: Perl code inside the Mojo::Template by haukex
in thread Perl code inside the Mojo::Template by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.