1

I have set transforms in admin but when I call those transforms in Twig nothing happens. 'getUrl('thumb')' shows blank in return although when calling like

 <img src="{{ asset.getUrl('thumb') }}"
  width="{{ asset.getWidth('thumb') }}"
  height="{{ asset.getHeight('thumb') }}">

width and height have values but url is blank. Anyone have idea?

1
  • Try wrapping that code in a conditional to check you have an asset, I suspect you don't. Commented Sep 10 at 12:37

2 Answers 2

2

I think you are missing the targeting of the asset itself.

If you are calling an Entry's asset from a field that expects one single image you can write:

{% set img = entry.fieldName.one() %}
<img 
   src="{{ img.getUrl('thumb') }}" 
   width="{{ img.getWidth('thumb') }}" 
   height="{{ img.getHeight('thumb') }}" />

If you are doing this in a for loop, or your field allows more than one image to be selected you can write:

{% for item in entry.fieldName %}
    {% set img = item.one() %}
    <img 
       src="{{ img.getUrl('thumb') }}" 
       width="{{ img.getWidth('thumb') }}" 
       height="{{ img.getHeight('thumb') }}" />
{% endfor %}
1

I think @kelly is correct, and you probably don't have an image to transform. I'd add to @kelly's suggestion a conditional to check, e.g:

{% set img = entry.fieldName.one() %}
{% if img %}
    <img 
        src="{{ img.getUrl('thumb') }}" 
        width="{{ img.getWidth('thumb') }}" 
        height="{{ img.getHeight('thumb') }}" />
{% else %}
    ! Oops! There is no image to transform!
{% endif%}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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