Answers.Body: <p>So in general, for things like this, I think the null coalescing operator in Twig is really useful. Something like <code>{% set myImage = entry.someImage ?? entry.someOtherImage ?? null %}</code> will set <code>myImage</code> to the first thing that is defined and is not null, from left to right.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-19T06:44:11.333Z
Answers.Text: Thanks. I think this is helpful in some cases, but doesn't help here. I just want to know, if I can access the property, and if it contains at least one time (a selected asset)
Answers.UserDisplayName: Matthias Redl-Mann
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-18T16:19:47.107Z
Answers.LastActivityDate: 2017-07-18T16:19:47.107Z
Answers.UserDisplayName: andrew.welch
Answers.Body: <p>Just found the trick: When you access an object in array style, you can test via <code>is defined</code></p>
  
  <p>e.g. </p>
  
  <pre><code>{{ object['property'] is defined ? 'defined' : 'not defined' }}
  </code></pre>
  
  <p>and you will get reliable results.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-07T16:23:30.487Z
Answers.Text: I would like to stress that checking for defined using the dot notation E.G. `{{ object.property is defined ? 'defined' : 'not defined' }}` is also possible. This works multiple levels deep.
Answers.UserDisplayName: Michel Rummens
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-20T14:22:14.757Z
Answers.LastActivityDate: 2017-07-20T14:22:14.757Z
Answers.UserDisplayName: Matthias Redl-Mann
Body: <p>On Craft 3 I am iterating over matrix elements and I need to check if a certain property "videoPoster" exists. It's an asset field, so I actually want to check its length, but I'am already failing to check if it's available...</p>
  
  <p>"ce" is one block of my matrix, this is what I'm doing:</p>
  
  <pre><code>{{ ce.videoPoster is defined ? 'yes' : 'no' }}
  </code></pre>
  
  <p>Works, gives me a nice "Yes"</p>
  
  <p>Anything else does not work:</p>
  
  <pre><code>{{ ce.videoPoster is defined and ce.videoPoster ? 'yes' : 'no' }}
  {{ ce.videoPoster is defined and ce.videoPoster is not null ? 'yes' : 'no' }}
  {{ ce.videoPoster is defined and ce.videoPoster | length ? 'yes' : 'no' }}
  </code></pre>
  
  <p>Exception: Calling unknown method: craft\elements\MatrixBlock::videoPoster()</p>
  
  <p>What is the correct check? I just want to know if I can call <code>ce.videoPost.one()</code></p>
  
Tags: <templating>
Title: How to check if object property exists
ThreadText: # How to check if object property exists
  
  (Asked by: username_0 on 2017-07-18)
  
  On Craft 3 I am iterating over matrix elements and I need to check if a certain property "videoPoster" exists. It's an asset field, so I actually want to check its length, but I'am already failing to check if it's available...
  
  "ce" is one block of my matrix, this is what I'm doing:
  
  ```
  {{ ce.videoPoster is defined ? 'yes' : 'no' }}
  
  ```
  
  Works, gives me a nice "Yes"
  
  Anything else does not work:
  
  ```
  {{ ce.videoPoster is defined and ce.videoPoster ? 'yes' : 'no' }}
  {{ ce.videoPoster is defined and ce.videoPoster is not null ? 'yes' : 'no' }}
  {{ ce.videoPoster is defined and ce.videoPoster | length ? 'yes' : 'no' }}
  
  ```
  
  Exception: Calling unknown method: craft\elements\MatrixBlock::videoPoster()
  
  What is the correct check? I just want to know if I can call `ce.videoPost.one()`
  
  **Tags:** templating
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2017-07-20. Score: 7 | Accepted answer
  
  Just found the trick: When you access an object in array style, you can test via `is defined`
  
  e.g.
  
  ```
  {{ object['property'] is defined ? 'defined' : 'not defined' }}
  
  ```
  
  and you will get reliable results.
  
  > **Comments:**
  > **username_2**: I would like to stress that checking for defined using the dot notation E.G. `{{ object.property is defined ? 'defined' : 'not defined' }}` is also possible. This works multiple levels deep.  
  
  ---
  
  ### Answer by username_1 on 2017-07-18. Score: 1
  
  So in general, for things like this, I think the null coalescing operator in Twig is really useful. Something like `{% set myImage = entry.someImage ?? entry.someOtherImage ?? null %}` will set `myImage` to the first thing that is defined and is not null, from left to right.
  
  > **Comments:**
  > **username_0**: Thanks. I think this is helpful in some cases, but doesn't help here. I just want to know, if I can access the property, and if it contains at least one time (a selected asset)  
  
  ---
  
  