xslate / p5-Text-Xslate

Scalable template engine for Perl5
https://metacpan.org/release/Text-Xslate
Other
121 stars 47 forks source link

Syntax::TTerse support for looping arrays #124

Closed abraxxa closed 10 years ago

abraxxa commented 10 years ago

I didn't find a way to loop an array, not arrayref, when using TTerse syntax with Catalyst::View::Xslate. I'm calling the 'all' method on a DBIx::Class resultset on the Catalyst stash which returns an array of DBIx::Class::Row objects which I want to loop over.

lestrrat commented 10 years ago

Untested. Does this work?

[% FOREACH row IN [ resultset.all() ] %]...[% END %]
abraxxa commented 10 years ago

Unfortunately that doesn't work either:

Text::Xslate: Cannot access 'id' (19 is not a container) (/home/ahartmai/perl5/git/NAC-Web-CIS/root/navi.tt:9) at /home/ahartmai/perl5/perlbrew/perls/20/lib/site_perl/5.20.0/Catalyst/Action.pm line 65.
----------------------------------------------------------------------------
    [% FOREACH devicegroup IN [ groups.all() ] %]
    <li[% IF navlist == devicegroup.id %] id="active"[% END %]><a href="[% c.uri_for_action(c.controller('Device').action_for('list'), [ devicegroup.id ]) %]">[% devicegroup.devicegroupname %]</a></li>
    [% END %] 
lestrrat commented 10 years ago

Yeah, I read the docs again, and xslate only allows scalar context on return values. I think you're just going to have to inject a all_arrayref() or something on your Resultset (DBIC allows you to do this, IIRC)

abraxxa commented 10 years ago

I've tried using Kolon and Metakolon and they also don't allow iterating over arrays returned from a has_many relationship or the all method. I'm giving up on Xslate as long as this feature isn't added as it's too much work for too less benefit. I was hoping to find a template language that is less hassle than TT but Xslate isn't any better at the moment for me.