1

I am new to Craft CMS and struggling to figure out how to display my logo in my template.

The error message is:

Variable "siteLogo" does not exist

This is the code I am using:

{% for asset in siteLogo.siteLogo %} 
    <img width="180px" src="{{ asset.url }}" alt="ICW Logo">
{% endfor %}

Any advice would be greatly appreciated!

Thanks in advance.

3

Welcome to Craft! Seeing as you are new I will try and explain the error, and offer some advice on how I would architect what you are trying to achieve:

Variable "siteLogo" does not exist.

This error is coming from the code sample you provided, more specifically it is referring to this part:

{% for asset in siteLogo.

Craft is looking for something called siteLogo. It's not looking for a field called siteLogo, rather a place where that field might exist.

I would assume your asset field exists inside a Global? That's how I would do it, but you just need to double check what the handle of your Global is, as that's what you use to access it in the templates. Also double check what the handle of your asset field is, as again, that's what you use in the templates to reference that field and get the asset in it.


So taking that, here is what I would do:

Firstly, I would use a Global, but I wouldn't call this "Site Logo". That is too specific. Maybe call it "Branding", as this opens up the possibility of adding more fields (Colour Scheme etc).

When you create a Global, Craft automatically generates the handle from the name you provide. A Global with a name of "Branding" would have a handle of branding.

Next I would create an Asset field, and call this "Site Logo". This handle would automatically generate to siteLogo (what you have already). You would also want to restrict this Asset field to 1 asset.

Now for the template.

As you only have 1 logo you do not need to do a forloop. Instead you can just output the 1 asset like so:

{% set logo = branding.siteLogo.one %}
{% if logo|length %}
    <img src="{{ logo.url }}">
{% endif %}

I hope that helps!

1
  • 1
    Thank you so much Jamie! Your explanation was great. I have been struggling to understand Craft but it feels good to finally make some progress. I know this is quite basic but hopefully I can start to understand more as I continue.
    – Jaffa
    Apr 16 '19 at 8:21

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.