1

just getting start with Craft, and I can't figure out why my images won't output. Any time I try to output even the path to the url of my image, I get an internal error: Craft\AssetFileModel and its behaviors do not have a method or closure named [...].

I can successfully do this:

{% if entry.pageImage | length %}
    {% for pageImage in entry.pageImage %}
        {{ pageImage }}
    {% endfor %}
{% endif %}

Which just outputs the title of the image. But I want to display the image. I was expecting to be able to be able to use {{pageImage.getUrl()}} or {{pageImage.url}} or {{pageImage.fileName}} but these will throw the aforementioned error. How do I output an image?

1 Answer 1

2

You would usually display the image with

{% for pageImage in entry.pageImage %}
    <img src="{{ pageImage.getUrl() }}"> 
{% endfor %}

But this only works if you enabled the public url option for the asset source. There is a light switch if those assets should have urls or not in your cp

It also has a getImg() function

/**
 * Returns an <img> tag based on this asset.
 *
 * @return \Twig_Markup|null
 */
public function getImg()
{
    if ($this->kind == 'image' && $this->getHasUrls())
    {
        $img = '<img src="'.$this->getUrl().'" width="'.$this->getWidth().'" height="'.$this->getHeight().'" alt="'.HtmlHelper::encode($this->title).'" />';
        return TemplateHelper::getRaw($img);
    }
}

Your Answer

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

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