I can't, for the life of me, find an answer to this question.

I have a global called work. It has three Asset fields. One is called mediakit, which is an upload of a PDF. I am trying to get the file name of that asset and add it into the template but cannot find anything giving me the proper way to do this in the template

Based on the documentation I thought:

 {{ work.mediakit }}

would output something at least but nothing shows and so whittling away from there to get it to call the correct path has eluded me.

Can anyone give me the correct way to call that?

The path is:

  "/assets/medikit/{{ the filename of the pdf uploaded}}"

Assets fields always return ElementCriteriaModel’s in templates. To get the actual related asset, do this:

{% set asset = work.mediakit.first() %}

You can’t be sure that there’s actually a related asset, so it’s worth making sure work.mediakit.first() actually returned something before accessing its properties:

{% if asset %}
    {{ asset.url }}
{% endif %}
  • 1
    "Asset fields are always returned as arrays in templates" - I think you just helped me level up (now at level 2!) Thanks a ton. Oct 13 '15 at 13:27
  • 1
    ...and because they are arrays, you can test if the field actually contains any images or not like this: {% if work.mediakit|length %} Oct 13 '15 at 18:02
  • 2
    Technically they return ElementCriteriaModel’s, not arrays, and grabbing the first asset with [0] could potentially be excessive if you have more than one asset and only need the first one (as this will actually load all of the assets behind the scenes, before returning the first one). Oct 16 '15 at 7:00

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.