Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Mojo Template, passing dynamic index to array.

by Anonymous Monk
on Jul 13, 2021 at 18:45 UTC ( [id://11134970]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks!!

I am trying to pass a dynamic value to print a value out of an array using Mojo::Template, getting syntax errors, is there a way to do that?

The code works if its like this:

<% $local->{ place }[ 0 ]{ street } %>
I'd like to pass the index number dynamic using the example below:

Code snipped from the .ep template file:

<p> Location: <br/> <% $local->{ place }[ %> <%= $number %> <% ]{ street } %> </p>
<%= $number %>, can be 0,1,2,3...

Replies are listed 'Best First'.
Re: Mojo Template, passing dynamic index to array.
by jdporter (Paladin) on Jul 13, 2021 at 19:02 UTC

    Did you try this? It's just one chunk of perl.

    <% $local->{ place }[ $number ]{ street } %>
Re: Mojo Template, passing dynamic index to array.
by LanX (Saint) on Jul 13, 2021 at 19:16 UTC
    According to Mojo::Template please try
    <p> Location: <br/> <%= $local->{place}[ $number ]{street} %> </p>

    > The code works if its like this: <% $local->{ place }[ 0 ]{ street } %>

    Unlikely, the = is missing in <%= to insert the value

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: Mojo Template, passing dynamic index to array.
by Anonymous Monk on Jul 14, 2021 at 07:08 UTC
    Why not keep it simple and pass $street?

      Not sure why this got downvoted, passing a few variables with the values for the 'row' in question in most cases makes more sense than passing an entire data structure then displaying one 'row' from a result set.

        I understood $number as loop var, loading it into a temporary $street var would lead to more code.

        YMMV...

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        Maybe because AnonyMonk in this case was inferring an XY problem when there really isn't one. The OP actually needs to know how to do what he was trying to do; offering him a kludgy workaround isn't doing him any favors.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11134970]
Approved by LanX
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-18 01:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found