<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>

CC BY-SA 3.0
2015-10-21T17:01:36.373Z
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.
Phil Gyford
CC BY-SA 3.0
2015-10-21T17:53:28.040Z
Ahh, good point. Let me think on it.
Brad Bell
CC BY-SA 3.0
2015-10-21T16:34:17.537Z
2015-10-21T16:34:17.537Z
2017-04-13T12:51:00.303Z
Brad Bell
<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>

CC BY-SA 3.0
2015-10-21T15:53:06.947Z
Thanks, but if I do that I still get the same error.
Phil Gyford
CC BY-SA 3.0
2015-10-21T13:26:41.763Z
2015-10-21T16:05:32.833Z
2015-10-21T16:05:32.833Z
Mike Pepper
<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>

<templating><globals>
How do I check if a Global field exists in a template?
# 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 Brad. 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():
{% if myGlobalSet.getAttribute('myField') %}
    Defined.
{% else %}
    Not defined.
{% endif %}

I was going to suggest BaseElementModel::getFieldValue() 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.  

---

