Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^5: Preparing data for Template

by hippo (Bishop)
on Dec 31, 2020 at 11:47 UTC ( [id://11126039]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Preparing data for Template
in thread Preparing data for Template

However, the values are not being passed back to the Template. Or, if they are, they are not getting displayed.

Here is an SSCCE to demonstrate looping over the return values:

#!/usr/bin/env perl use strict; use warnings; use Template; sub baz { return qw/there World/; } my $v = { bar => \&baz }; my $t = join '', <DATA>; Template->new->process (\$t, $v); __DATA__ [% FOREACH foo IN bar() %] Hello [% foo %]! [% END %]

🦛

Replies are listed 'Best First'.
Re^6: Preparing data for Template
by Bod (Parson) on Dec 31, 2020 at 12:41 UTC

    Thanks hippo.
    Based on your code sample and what I already have, I have built a test which shows it is not the way I am using Template that is the issue - it is my display() function. In the 'real' code this is abstracted into a Site::HTML module because I want to hide the site variables away in a separate module with only Site::HTML and Site::Common having access to it - perhaps I need to rethink that design.

    use Template; use strict; my $template = Template->new; my $vars = { 'frames' => \&get_frames }; $template->process('test.tt', $vars); # this works display('test', $vars); # this doesn't sub get_frames { my @list; foreach my $l( qw/first second/ ) { my $fr = { 'one' => $l, 'two' => $l, }; push @list, $fr; } return @list; } sub display { # my $self = shift; my $file = shift; my %vars = @_; $template->process("$file.tt", \%vars); }

    update

    The test Template...

    Start test... [% FOREACH frame IN frames() %] Frame - [% frame.one %] [% frame.two %] [% END %] End test...

      You are passing a hash reference:

      display('test', $vars);
      ... but you are expecting a "hash" (well, list of pairs):

      sub display { # my $self = shift; my $file = shift; my %vars = @_;

      Most likely in display, you want instead:

      sub display { my( $file, $vars ) = @_; $template->process("$file.tt", $vars); }
        You are passing a hash reference ... but you are expecting a "hash" (well, list of pairs)

        Yes - of course you are totally correct...
        My inability to properly conceptualise references strikes again :(

        I seem incapable of truly internalising references, especially when they are being passed between distinct lumps of code. Despite reading, understanding and implementing, when I come to create new code that passes references I always seem to run into trouble. I don't think it's a lack of knowledge, it is a lack of understanding that repeatedly causes problems similar to this one.

        Any thoughts on how to get over this mental block would be appreciated.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-03-29 12:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found