I am using a global set to store image assets used across the site. I am not sure how to reference a specific image. I know how to get the image if its just one, using :

{{ design.imgs | length ? design.imgs.first().getUrl() : '' }}

What if there are several images and I am looking for a specific one?

  • 1
    You could loop through them. But what distinguishes this specific one? Aug 16 '15 at 2:55
  • The loop will return all of the images, correct? I am storing a bunch of images in the global and always need to call just one at a time - like the logo. Each one has a different asset name. ( filename or title ) Aug 16 '15 at 3:02
  • 1
    The loop will return all of them, one at a time. If you have a fixed set of specific images (logo, ...), I would make a global asset for each one. So design.logo would be your one logo image. If you are trying to do something else, please explain. Aug 16 '15 at 6:19
  • Looks like, when using global sets, you can either make a field for every specific item ( like in your example above - design.logo ) or throw all the assets into one field ( imgs in my case). Looks like your methodology is far simpler. Is throwing all items into one field and searching for them with the methodology suggested by Lindsey D below unnecessarily complicated ? Aug 16 '15 at 11:40
  • If you know in advance what all the specific items are, and there aren't more than maybe a dozen of them, then I would say my methodology is simpler. But otherwise you may need to go with searching. And you could throw some together in one field, and make specific fileds for some others. Aug 16 '15 at 18:22

When you call myGlobalSet.myAssetsField, you're creating an ElementCriteriaModel. This is significant, because an ElementCriteriaModel is basically a "search object".

Your design.imgs call produces an ElementCriteriaModel based on craft.assets. It will return only the assets which are related to your global field.

You can manipulate your ElementCriteriaModel in whatever way you want!

{% set myAsset = design.imgs.kind('image').sourceId(1).first %}
  • Lindsey - I am looking to output the url for the specific image. not sure how to do that with your example above. Aug 16 '15 at 11:53
  • Quite easily in fact... myAsset.getUrl()
    – Lindsey D
    Aug 16 '15 at 16:05
  • 1
    @user2022284 - You need some identifiable piece of information about the file you're looking for, like its file name or asset ID, etc. so you can tell Craft what to search for. Do you know any of those ahead of time?
    – Brad Bell
    Aug 16 '15 at 16:35
  • Hi Brad : I know the image name and title - both of which will be unique across all the images. Aug 16 '15 at 21:49
  • @user2022284: If you know the image's name & title, why not just access it directly through craft.assets? There's no need to refer to the global field at all if you already know which asset you want.
    – Lindsey D
    Aug 16 '15 at 21:57

If you have a smallish (under a dozen or so), pre-determined set of distinguished images, then I would create a separate global asset for each of them. So you would have design.logo, design.facebookIcon, etc. And then just refer to them that way:

{% if design.logo|length %}
  {{ design.logo.first.getUrl() }}
{% endif %}

Always check that an asset exists before you use it. Otherwise you will have your site front-end mysteriously crashing at some point in the future when that asset is not there

  • Hi Marion, Yes, this works for the scenario you describe . Perhaps it easier this way. But will like to still learn how to do so if I were to dump everything in one field - with image name and title being the known element , then search for it. Aug 16 '15 at 21:51

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.