If statement for missing variable content

Hi, I’m trying to configure some dynamic schema for a section of our site. The schema type I am using requires an img URL to be set. This is fine and straightforward when the page has a featured image, but when it doesn’t the markup returns an error for the missing image.

I have pretty much 0 experience with hubl and just sort of learning as I go, so my question: Is it possible to create some kind of conditional variable/if statement to substitute the featured image variable if it’s not present?

this is the pertinent section of the code:

“image”: {
“@type”: “ImageObject”,
“url”: “{{ content.featured_image }}”
},

Where I want to do something like if content.featured_image is not found use site_settings.logo_src or some absolute URL.

Thanks in advance!

I found this code snippet provided by Stephanie that should achieve the result you are looking for Set a default featured image on your HubSpot site

Add to the HEAD of your template

{% if !content.featured_image %}
<meta property="og:image" content="https://www.yourdomain.com/hubfs/yourimage.jpg" />
{% endif %}

Jacob Lett

Freelance HubSpot CMS Developer & Web Designer



28844i368E9AAC739341A8.png

Located in Michigan, USA

Get a free estimate now ›


Hi @MN1,

@Jake_Lett is right. For your specific code it would be:

"image": {
 "@type": "ImageObject",
 "url": "{% if content.featured_image %}{{ content.featured_image }}{% else %}[YOUR LOGO URL]{% endif %}"
},

You might need to make {{ content.featured_image }} into {{ content.featured_image.url }} as an image in HubL has a few properties e.g. height, width, alt, and url.

Bingo, thank you both! Was obviously way overthinking this. Much appreciated.