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

Ability to loop through containers containing primitives in Silverstripe templates #11196

Closed
chrispenny opened this issue Apr 10, 2024 · 4 comments

Comments

@chrispenny
Copy link
Contributor

chrispenny commented Apr 10, 2024

Description

It would be great if we were able to pass simple numeric/non-associative arrays to Silverstripe templates for them to be output. MVP for me would also be when those arrays only contain simple data types (string, int, float, etc).

I think this is probably felt most (for me) when working with external data sources. Trying to output some simple indexed content all of a sudden requires a bunch of processing before you can return it to your template.

As an aside, in Silverstripe 3, this would work:

class MyController extends PageController
{
    public function ArrayListTest()
    {
        return new ArrayList(['item1', 'item2', 'item3']);
    }
}
<% loop $ArrayListTest %>
    $Me
<% end_loop %>

But now in Silverstripe 4 and 5, this results in an error:

[Emergency] Uncaught Error: Call to a member function XML_val() on string

Noting though, that I'd probably prefer to avoid needing to convert my array to an ArrayList, as that still requires processing.

Additional context or points of discussion

No response

Acceptance criteria

  • It's possible for a developer to create an list of scalar values to be rendered in an SS template withouth the developer having to warp them in an ArrayData construct.
  • Individual scalar value are escaped when rendered to eliminate any XSS risk.
  • Primitive arrays can be passed to templates and can be looped through

New issues created

Kitchen-sink CI run

PRs

@GuySartorelli
Copy link
Member

This is a good idea. The template engine should be able to check if the value it's operating on is an object or not, and if it's not an object give it the appropriate DBField casting (bool to DBBoolean, etc)

@GuySartorelli GuySartorelli changed the title Ability to loop through numeric/un-associative arrays in Silverstripe templates Ability to loop through arrays or primitives in Silverstripe templates Apr 11, 2024
@GuySartorelli GuySartorelli changed the title Ability to loop through arrays or primitives in Silverstripe templates Ability to loop through arrays containing primitives in Silverstripe templates Apr 11, 2024
@maxime-rainville
Copy link
Contributor

@GuySartorelli added this to the 5.3 milestone.

This looks like the potential for a quick win.

I added a very generic AC just to avoid pre-judging of the best way of doing this

I'm thinking of the top of my head:

  • We could try to update the rendering logic so it can understand scalar values or array of scalar value implicitly. While this may be the most intuitive solution for the end developer, I suspect it might also be the most difficult to implement.
  • Maybe we could update the SS Viewer logic so it automatically wrap up any scalar value it gets into some viewable data wrapper.
  • Maybe we update ArrayList so when you give it a scalar value, it wraps it in a viewable data wrapper.

@GuySartorelli
Copy link
Member

GuySartorelli commented Apr 11, 2024

The first two options there are just an implementation detail, really. Whether SSViewer (or the template parser, etc etc) wraps the primitive or just deals with it directly doesn't really matter - what matters is that developers don't have to do that part themselves.

That third option would have to be in a major release, and goes in the wrong direction imo. I'd prefer to see ArrayList not wrap any values in ArrayData at all, even in the cases it currently does.

@emteknetnz emteknetnz self-assigned this May 9, 2024
@emteknetnz emteknetnz removed their assignment May 13, 2024
@GuySartorelli GuySartorelli changed the title Ability to loop through arrays containing primitives in Silverstripe templates Ability to loop through containers containing primitives in Silverstripe templates May 14, 2024
@emteknetnz emteknetnz removed their assignment May 15, 2024
@GuySartorelli
Copy link
Member

PRs merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants