1

I've setup a global asset field so that a client can change a placeholder image that gets used throughout their website. I'm using the Imager plugin for my image transforms.

The following image code works fine when using a hard-coded image URL:

{% set image = 'http://placehold.it/450x225/eee/eee' %}
{% set transformedImage = craft.imager.transformImage(image, { width: 450, height: 225, }) %}
<img src="{{ transformedImage.url }}" />

But when I replace the hard-coded image URL with the global asset field, it doesn't work:

{% set image = '{{ websitePreferences.placeholderImage.first.url }}' %}
{% set transformedImage = craft.imager.transformImage(image, { width: 450, height: 225, }) %}
<img src="{{ transformedImage.url }}" />

I get this error message (Craft/Exception):

Source folder “/Users/stephen/Repositories/repo-name-here/public_html./” does not exist

If I output the global asset field elsewhere in the template, I get this:

http://my-locl-url.dev/uploads/images/Placeholders/placeholder-image.png

If I hard-code this URL into my image code, the image works fine. So does anyone have any idea why using a global for the placeholder image in this way isn't working? Thanks for any help.

3

I don't know if it's the cause of the error you're receiving or not, but this is incorrect Twig syntax:

{% set image = '{{ websitePreferences.placeholderImage.first.url }}' %}

You'll never need to nest {{ }} inside of {% %} or visa versa in Twig.

This is correct:

{% set image = websitePreferences.placeholderImage.first() %}
| improve this answer | |
  • I try the above >>>> {% set image = websitePreferences.placeholderImage.first() %} and I doesn't work for me. it only output the name of the image "<img src="Craft Cms Vs Wordpress Card">" – user3188831 Sep 14 '18 at 20:50

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.