Answers.Body: <p>you can use the <a href="https://docs.craftcms.com/v3/dev/tests.html#instance-of" rel="nofollow noreferrer">instance of</a> twig test here.</p>
  
  <p>You want to flip your logic, so test for Query object first else array stuff but this should work I think.</p>
  
  <p>To get the specific class just <code>{% dd props.cards %}</code> in the template for ease.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-10T14:25:40.593Z
Answers.Text: 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!
Answers.UserDisplayName: Greg Bowne
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-08T16:03:48.733Z
Answers.LastActivityDate: 2020-06-08T16:03:48.733Z
Answers.UserDisplayName: billythekid
Answers.Body: <p>If you have a custom module/plugin, you can <a href="https://craftcms.com/docs/3.x/extend/extending-twig.html#register-a-twig-extension" rel="nofollow noreferrer">register a Twig extension</a>, and then your class would look something like this:</p>
  <pre><code>&lt;?php
  
  namespace my\plugin\twigextensions;
  
  use Twig\Extension\AbstractExtension;
  use Twig\TwigFilter;
  use Twig\TwigFunction;
  
  
  class MyTwigExtension extends AbstractExtension
  {
      /**
       * @inheritdoc
       */
      public function getFilters(): array
      {
          return [
              new TwigFilter('isArray', [$this, 'isArray']),
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function getFunctions(): array
      {
          return [
              new TwigFunction('isArray', [$this, 'isArray']),
          ];
      }
  
      /**
       * Twig function to test if something is an array or not.
       *
       * This is useful for checking if something has been eager loaded or just a db query
       * class that needs to be executed.
       *
       * @param mixed $variable
       * @return bool
       */
      public function isArray($variable): bool
      {
          return is_array($variable);
      }
  }
  </code></pre>
  <p>Now you can do this in your templates:</p>
  <pre><code>{% if isArray(props.cards) %}
  ...
  </code></pre>
  <p>or</p>
  <pre><code>{% if props.cards|isArray %}
  ...
  </code></pre>
  <p>The benefit of doing it this way is you don't need to know the db query class of your field. I find this to be more flexible for checking if something's been eager loaded or not, although it does require you to have a custom module set up for it to work, which in almost any site I work on that's the case anyway. Just another option.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-19T16:24:59.160Z
Answers.LastActivityDate: 2021-08-19T16:24:59.160Z
Answers.UserDisplayName: Ryan
Body: <p>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.</p>
  
  <p>If something is essentially an element, we need to do a <code>.all()</code> to not have deprecation warnings, but if you do that on an array, you get errors. I've read through <a href="https://craftcms.stackexchange.com/questions/26426/for-loop-that-can-accept-array-and-element-query-without-throwing-all-deprecati">A similar question with element queries vs arrays</a> but since I'm not explicitly running a query on the page, this isn't quite applicable. </p>
  
  <p>Both arrays and objects are <code>iterable</code> so that can't distinguish it. Ideally I'd like to do something like this:</p>
  
  <pre><code>{% if props.cards is array %}
      {% set cardArray = props.cards %}
  {% else %}
      {% set cardArray = props.cards.all() %}
  {% endif %}
  {% for card in cardArray %}
  ...
  </code></pre>
  
  <p>Obviously, I'd be fine swapping the if/else conditions for the right test case</p>
  
Tags: <array><element><deprecation>
Title: How can I tell if a variable is an array or an entry/element query?
ThreadText: # How can I tell if a variable is an array or an entry/element query?
  
  (Asked by: username_0 on 2020-06-08)
  
  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](https://craftcms.stackexchange.com/questions/26426/for-loop-that-can-accept-array-and-element-query-without-throwing-all-deprecati) 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
  
  **Tags:** array, element, deprecation
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-06-08. Score: 4
  
  you can use the [instance of](https://docs.craftcms.com/v3/dev/tests.html#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.
  
  > **Comments:**
  > **username_0**: 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!  
  
  ---
  
  ### Answer by username_2 on 2021-08-19. Score: 1
  
  If you have a custom module/plugin, you can [register a Twig extension](https://craftcms.com/docs/3.x/extend/extending-twig.html#register-a-twig-extension), and then your class would look something like this:
  
  ```
  <?php
  
  namespace my\plugin\twigextensions;
  
  use Twig\Extension\AbstractExtension;
  use Twig\TwigFilter;
  use Twig\TwigFunction;
  
  class MyTwigExtension extends AbstractExtension
  {
      /**
       * @inheritdoc
       */
      public function getFilters(): array
      {
          return [
              new TwigFilter('isArray', [$this, 'isArray']),
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function getFunctions(): array
      {
          return [
              new TwigFunction('isArray', [$this, 'isArray']),
          ];
      }
  
      /**
       * Twig function to test if something is an array or not.
       *
       * This is useful for checking if something has been eager loaded or just a db query
       * class that needs to be executed.
       *
       * @param mixed $variable
       * @return bool
       */
      public function isArray($variable): bool
      {
          return is_array($variable);
      }
  }
  
  ```
  
  Now you can do this in your templates:
  
  ```
  {% if isArray(props.cards) %}
  ...
  
  ```
  
  or
  
  ```
  {% if props.cards|isArray %}
  ...
  
  ```
  
  The benefit of doing it this way is you don't need to know the db query class of your field. I find this to be more flexible for checking if something's been eager loaded or not, although it does require you to have a custom module set up for it to work, which in almost any site I work on that's the case anyway. Just another option.
  
  
  ---
  
  