If statement for missing variable content

SOLVE
Highlighted
New Contributor

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!

Reply
0 Upvotes
1 Accepted solution

Accepted Solutions
Highlighted
Top Contributor

Hi @MN1,

 

@BootstrapC 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.

3 Replies 3
Highlighted
Regular Contributor

I found this code snippet provided by Stephanie that should achieve the result you are looking for https://www.stephanieogaygarcia.com/hubspot-website-development/set-a-default-featured-image-on-your...

 

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


bootstrapcreative-horizontal-trim.png

Located in Michigan, USA

Get a free estimate now


Highlighted
Top Contributor

Hi @MN1,

 

@BootstrapC 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.

Highlighted
New Contributor

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

Reply
0 Upvotes