Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Preparing data for Template

by 1nickt (Canon)
on Dec 30, 2020 at 23:50 UTC ( [id://11126018]=note: print w/replies, xml ) Need Help??


in reply to Preparing data for Template

Hi,

"I'm not sure when list_frames is supposed to get called..."

It won't get called unless you call it ;-)

'frames' => \&list_frames,
This creates a reference ...

To simply call the sub you just want:

'frames' => list_frames(),

See also: https://www.oreilly.com/library/view/advanced-perl-programming/1565922204/ch04.html

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Preparing data for Template
by Bod (Parson) on Dec 31, 2020 at 00:21 UTC
    It won't get called unless you call it ;-)

    I was kind of assuming that the Template code calls the subroutine at some point! After all, it gets passed the reference...

    In the example code, get_user_projects doesn't seem to be explicitly called.

    #!/usr/bin/perl use strict; use warnings; use Template; use CGI; $| = 1; print "Content-type: text/html\n\n"; my $file = 'userinfo.html'; my $vars = { 'version' => 3.14, 'days' => [ qw( mon tue wed thu fri sat sun ) ], 'worklist' => \&get_user_projects, 'cgi' => CGI->new(), 'me' => { 'id' => 'abw', 'name' => 'Andy Wardley', }, }; sub get_user_projects { my $user = shift; my @projects = ... # do something to retrieve data return \@projects; } my $template = Template->new({ INCLUDE_PATH => '/home/abw/websrc/src:/home/abw/websrc/lib', PRE_PROCESS => 'config', }); $template->process($file, $vars) || die $template->error();

      In the example code, get_user_projects doesn't seem to be explicitly called.

      Not in the Perl. But in the template, the line [% FOREACH project IN worklist(me.id) %] calls worklist as a function with the appropriate ID as its argument.

      I would assume that in your code, if you don't change to 'frames' => list_frames() (as already suggested), then you would need to call the frames as a function rather than treating it as a variable: probably like [% FOREACH frame IN frames() %] (untested)

      (But it seems to me that you shouldn't need to call a function from the template, unless the data will change based on some parameter from the template, like the me.id argument in the tutorial example. Since you're not feeding anything back to the function, why not just define frames as the arrayref rather than the coderef?)

        Not in the Perl. But in the template, the line [% FOREACH project IN worklist(me.id) %] calls worklist as a function with the appropriate ID as its argument.

        That confirms what I suspected - thanks

        (But it seems to me that you shouldn't need to call a function from the template, unless the data will change based on some parameter from the template, like the me.id argument in the tutorial example. Since you're not feeding anything back to the function, why not just define frames as the arrayref rather than the coderef?)

        This is a learning exercise rather than a complete solution. Totally agreed that a function is not needed here yet, but I'm trying to learn how to use functions within Templates so that, when I need them, I have a good grasp of how to implement them.

        Update:

        probably like [% FOREACH frame IN frames() %] (untested)

        Yes - that is the part I omitted...thanks, that gets me further.
        The subroutine is now being called. However, the values are not being passed back to the Template. Or, if they are, they are not getting displayed.

Re^2: Preparing data for Template
by Bod (Parson) on Dec 31, 2020 at 11:17 UTC
    Hope this helps!

    Yes thank you, it does help a lot.

    However, it makes me wonder how the code in the example works...I was trying to replicate this arrangement as a learning exercise to understand dynamically preparing data based on the template file.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11126018]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-24 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found