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


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 %}
  <meta(name='twitter:image:', content='{{ seo.twittercard.first.getUrl() }}')>
{% endif %}

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

  • 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') }}') Aug 14 '15 at 18:30
  • 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. Aug 14 '15 at 18:49
  • 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') }}') Aug 14 '15 at 18:53
  • 1
    oops! fixing my answer. You want {{seo.twittercard.first.getUrl()}} Aug 14 '15 at 19:00
  • boom! works nicely! Thx. Aug 15 '15 at 3:13

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

<img src="{{ globalHandle.globalAssetHandle.one().getUrl }}" alt="">

You have to set your global handle here:

enter image description here

And your global asset handle here:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.