1

We have some components that are used to build pages, like normal, and some where we hydrate them with other data, so we are passing in arrays instead of entries.

If something is essentially an element, we need to do a .all() to not have deprecation warnings, but if you do that on an array, you get errors. I've read through A similar question with element queries vs arrays but since I'm not explicitly running a query on the page, this isn't quite applicable.

Both arrays and objects are iterable so that can't distinguish it. Ideally I'd like to do something like this:

{% if props.cards is array %}
    {% set cardArray = props.cards %}
{% else %}
    {% set cardArray = props.cards.all() %}
{% endif %}
{% for card in cardArray %}
...

Obviously, I'd be fine swapping the if/else conditions for the right test case

2

you can use the instance of twig test here.

You want to flip your logic, so test for Query object first else array stuff but this should work I think.

To get the specific class just {% dd props.cards %} in the template for ease.

| improve this answer | |
  • This worked like a charm for me. I did have to double escape the back slashes when single quoting the string, and I found two different classes that were possible on one component, because of good code reuse, but problem solved! – Greg Bowne Jun 10 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.