Answers.Body: <p>That's a known issue with Twig's <code>is defined</code> test.  See <a href="https://craftcms.stackexchange.com/a/2117/57">here</a> for an explanation and workarounds.</p>
  
  <p>Note that this is no longer an issue in the upcoming Craft 3.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-21T17:01:36.373Z
Answers.Text: Ah, thanks Brad. How do I use that workaround with a global rather than an entry though? `myGlobalSet.type` and `myGlobalSet.myField.type` both generate errors.
Answers.UserDisplayName: Phil Gyford
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-21T17:53:28.040Z
Answers.Text: Ahh, good point. Let me think on it.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-21T16:34:17.537Z
Answers.LastActivityDate: 2015-10-21T16:34:17.537Z
Answers.LastEditDate: 2017-04-13T12:51:00.303Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>Try using <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#getFieldValue-detail" rel="nofollow"><code>BaseModel::getAttribute()</code></a>:</p>
  
  <pre><code>{% if myGlobalSet.getAttribute('myField') %}
      Defined.
  {% else %}
      Not defined.
  {% endif %}
  </code></pre>
  
  <p>I was going to suggest <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#getFieldValue-detail" rel="nofollow"><code>BaseElementModel::getFieldValue()</code></a> as the "proper" way, but that throws an exception if the field isn't found <em>at all</em>, so I think <code>getAttribute()</code> is your best bet.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-21T15:53:06.947Z
Answers.Text: Thanks, but if I do that I still get the same error.
Answers.UserDisplayName: Phil Gyford
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-21T13:26:41.763Z
Answers.LastActivityDate: 2015-10-21T16:05:32.833Z
Answers.LastEditDate: 2015-10-21T16:05:32.833Z
Answers.UserDisplayName: Mike Pepper
Body: <p>In a template, can I check whether a Global field exists, before I try to output its value? I want to ensure the page doesn't break entirely in the unlikely event of the field being removed from the CMS.</p>
  
  <p>If I do:</p>
  
  <pre><code>{% if myGlobalSet.myField is defined %}
      Defined.
  {% endif %}
  </code></pre>
  
  <p>then it works as expected; "Defined" isn't displayed if <code>myField</code> doesn't exist.</p>
  
  <p>But if I have:</p>
  
  <pre><code>{% if myGlobalSet.myField is defined %}
      Defined. {{ myGlobalSet.myField }}
  {% endif %}
  </code></pre>
  
  <p>I get:</p>
  
  <blockquote>
    <p>Craft\GlobalSetModel and its behaviours do not have a method or closure named "myField".</p>
  </blockquote>
  
  <p>if that field doesn't exist. It's like it's trying to process the field even though it's not defined.</p>
  
Tags: <templating><globals>
Title: How do I check if a Global field exists in a template?
ThreadText: # How do I check if a Global field exists in a template?
  
  (Asked by: username_0 on 2015-10-21)
  
  In a template, can I check whether a Global field exists, before I try to output its value? I want to ensure the page doesn't break entirely in the unlikely event of the field being removed from the CMS.
  
  If I do:
  
  ```
  {% if myGlobalSet.myField is defined %}
      Defined.
  {% endif %}
  
  ```
  
  then it works as expected; "Defined" isn't displayed if `myField` doesn't exist.
  
  But if I have:
  
  ```
  {% if myGlobalSet.myField is defined %}
      Defined. {{ myGlobalSet.myField }}
  {% endif %}
  
  ```
  
  I get:
  
  > Craft\GlobalSetModel and its behaviours do not have a method or closure named "myField".
  
  if that field doesn't exist. It's like it's trying to process the field even though it's not defined.
  
  **Tags:** templating, globals
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-10-21. Score: 1
  
  That's a known issue with Twig's `is defined` test. See [here](https://craftcms.stackexchange.com/a/2117/57) for an explanation and workarounds.
  
  Note that this is no longer an issue in the upcoming Craft 3.
  
  > **Comments:**
  > **username_0**: Ah, thanks username_1. How do I use that workaround with a global rather than an entry though? `myGlobalSet.type` and `myGlobalSet.myField.type` both generate errors.  
  > **username_1**: Ahh, good point. Let me think on it.  
  
  ---
  
  ### Answer by username_2 on 2015-10-21. Score: 0
  
  Try using [`BaseModel::getAttribute()`](http://buildwithcraft.com/classreference/models/BaseElementModel#getFieldValue-detail):
  
  ```
  {% if myGlobalSet.getAttribute('myField') %}
      Defined.
  {% else %}
      Not defined.
  {% endif %}
  
  ```
  
  I was going to suggest [`BaseElementModel::getFieldValue()`](http://buildwithcraft.com/classreference/models/BaseElementModel#getFieldValue-detail) as the "proper" way, but that throws an exception if the field isn't found *at all*, so I think `getAttribute()` is your best bet.
  
  > **Comments:**
  > **username_0**: Thanks, but if I do that I still get the same error.  
  
  ---
  
  