<p><code>seo.twittercard</code> is an <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel" rel="noreferrer">ElementCriteriaModel</a>. To access the actual asset, you need to use <code>seo.twittercard.first</code> (or equivalently: <code>seo.twittercard.first()</code>).</p>

<p>However, you should always check that this exists before referencing it:</p>

<pre><code>{% if seo.twittercard|length %}
  &lt;meta(name='twitter:image:', content='{{ seo.twittercard.first.getUrl() }}')&gt;
{% endif %}
</code></pre>

<p>And yes, this is a fine way to store images used globally across the site.</p>

CC BY-SA 3.0
2015-08-14T18:30:54.857Z
What is the benefit of doing the check ( if the image exists?). I am hoping to simplify the code by doing something like this ?    meta(name='twitter:image:src', content='{{ image.getUrl('seo.twittercard.first') }}')
user2022284
CC BY-SA 3.0
2015-08-14T18:49:01.150Z
The image might not exist. It might not have been initialized, or the asset might have been removed. And then your site will be mysteriously broken. I think it is always worth checking.
Marion Newlevant
CC BY-SA 3.0
2015-08-14T18:53:18.190Z
Thx. Will do then. However, I tried to see if i can simply output the url on the template ( before putting it in the wrapper to check if it exists ). this is not working:   {{ image.getUrl('seo.twittercard.first') }}')
user2022284
CC BY-SA 3.0
2015-08-14T19:00:29.410Z
oops! fixing my answer. You want `{{seo.twittercard.first.getUrl()}}`
Marion Newlevant
CC BY-SA 3.0
2015-08-15T03:13:08.817Z
boom! works nicely! Thx.
user2022284
CC BY-SA 3.0
2015-08-14T18:25:02.180Z
2015-08-14T19:02:02.820Z
2015-08-14T19:02:02.820Z
Marion Newlevant
<p>This is a general structure to display global images in any template:</p>
<pre><code>&lt;img src=&quot;{{ globalHandle.globalAssetHandle.one().getUrl }}&quot; alt=&quot;&quot;&gt;
</code></pre>
<p>You have to set your global handle here:</p>
<p><a href="https://i.sstatic.net/NFDwS.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/NFDwS.png" alt="enter image description here" /></a></p>
<p>And your global asset handle here:</p>
<p><a href="https://i.sstatic.net/iDFYU.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/iDFYU.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2020-12-15T21:14:26.277Z
2020-12-15T21:14:26.277Z
Alexis Dalai Waldo Jiménez
<p>I have setup a global set for images i use across the site. I have one for seo - where I have images for a twitter card and Facebook image in my seo settings. There is only one image for each field ( so hoping to avoid using the array syntax )</p>

<p>Two questions:</p>

<ol>
<li><p>I am not sure how to access the images uploaded into this global variable. My global set has a handle "seo" and the image's handle is "twittercard". This isn't working :</p>

<p>meta(name='twitter:image:', content='{{ image.getUrl('seo.twittercard') }}')</p></li>
<li><p>Is this the most efficient way to store images used globally across the site? ( via global image variables?)</p></li>
</ol>

<templating><assets><image><globals>
Global Image Variables?
# Global Image Variables?

(Asked by: username_0 on 2015-08-14)

I have setup a global set for images i use across the site. I have one for seo - where I have images for a twitter card and Facebook image in my seo settings. There is only one image for each field ( so hoping to avoid using the array syntax )

Two questions:

1. I am not sure how to access the images uploaded into this global variable. My global set has a handle "seo" and the image's handle is "twittercard". This isn't working :

   meta(name='twitter:image:', content='{{ image.getUrl('seo.twittercard') }}')
2. Is this the most efficient way to store images used globally across the site? ( via global image variables?)

**Tags:** templating, assets, image, globals


---

## 2 answers

### Answer by username_1 on 2015-08-14. Score: 5 | Accepted answer

seo.twittercard is an ElementCriteriaModel. To access the actual asset, you need to use seo.twittercard.first (or equivalently: seo.twittercard.first()).
However, you should always check that this exists before referencing it:
{% if seo.twittercard|length %}
  
{% endif %}

And yes, this is a fine way to store images used globally across the site.

> **Comments:**
> **username_0**: What is the benefit of doing the check ( if the image exists?). I am hoping to simplify the code by doing something like this ?    meta(name='twitter:image:src', content='{{ image.getUrl('seo.twittercard.first') }}')  
> **username_1**: The image might not exist. It might not have been initialized, or the asset might have been removed. And then your site will be mysteriously broken. I think it is always worth checking.  
> **username_0**: Thx. Will do then. However, I tried to see if i can simply output the url on the template ( before putting it in the wrapper to check if it exists ). this is not working:   {{ image.getUrl('seo.twittercard.first') }}')  
> **username_1**: oops! fixing my answer. You want `{{seo.twittercard.first.getUrl()}}`  
> **username_0**: boom! works nicely! Thx.  

---

### Answer by username_2 on 2020-12-15. Score: 3

This is a general structure to display global images in any template:


You have to set your global handle here:

And your global asset handle here:


---

