Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't iterate on CONSTANTS #207

Open
assistcontrol opened this issue Nov 15, 2018 · 6 comments
Open

Can't iterate on CONSTANTS #207

assistcontrol opened this issue Nov 15, 2018 · 6 comments
Labels
Question / Help Help / question on TT usage

Comments

@assistcontrol
Copy link

I am having trouble iterating on CONSTANTS.

If I pass in:
CONSTANTS: { foo => [1, 2, 3] }

Then:

  [% constants.foo.size %]
  [% FOREACH constants.foo %]
    Hello
  [% END %]

Should print:

  3 Hello Hello Hello

Instead, it just prints: 3

Interestingly, [% foo.first %] works. I'm seeing the same problem iterating hashrefs.

What am I missing about CONSTANTS behaviour?

@atoomic atoomic added the Question / Help Help / question on TT usage label Dec 26, 2018
@gflohr
Copy link
Contributor

gflohr commented Feb 15, 2019

This looks like a syntax error. Try [% FOREACH item IN constants.foo %].

You probably get a quicker answer to this type of question on stackoverflow: https://stackoverflow.com/questions/tagged/template-toolkit

@assistcontrol
Copy link
Author

That doesn't work either.

[% constants.foo.size %]
[% FOREACH item IN constants.foo %]
  Hello
[% END %]

Output: 3

@gflohr
Copy link
Contributor

gflohr commented Feb 15, 2019

[% FOREACH item IN constants.foo %]
Hello
[% END %]

Then your Perl code that invokes the template processor has a bug. Please post it here. You are doing something like:

$constants->{foo} = @items;

When you should do:

$constants->{foo} = [@items];

If that doesn't help, please post your complete code or a reference to it.

This here works, by the way:

[% constants.foo = ['foo', 'bar', 'baz'] %]
[% FOREACH item IN constants.foo %]
  Hello
[% END %]

@assistcontrol
Copy link
Author

I appreciate all your help here, @gflohr!

This is what I'm using:

use Template;

my $T = Template->new({
    CONSTANTS => { foo => [1, 2, 3] }
});

$T->process(\*DATA, { alist => [4, 5, 6] });

__END__
Constant:

  Size: [% constants.foo.size %]
  Bare: [% FOREACH constants.foo %] Hello [% END %]
  Iter: [% FOR item IN constants.foo %] Hello [% END %]

Var:

  Size: [% alist.size %]
  Bare: [% FOREACH alist %] Hello [% END %]
  Iter: [% FOR item IN alist %] Hello [% END %]

@gflohr
Copy link
Contributor

gflohr commented Feb 15, 2019

Sorry, I didn't even know about the options CONSTANTS and CONSTANTS_NAMESPACE. I can try to have a look but I'm afraid that won't happen before Monday.

@assistcontrol
Copy link
Author

Any thoughts on this? This seems like a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question / Help Help / question on TT usage
Projects
None yet
Development

No branches or pull requests

3 participants