CMS Development

MN1
Participant

If statement for missing variable content

SOLVE

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!

0 Upvotes
1 Accepted solution
piersg
Solution
Key Advisor

If statement for missing variable content

SOLVE

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.

View solution in original post

3 Replies 3
piersg
Solution
Key Advisor

If statement for missing variable content

SOLVE

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.

MN1
Participant

If statement for missing variable content

SOLVE

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

0 Upvotes
Jake_Lett
Guide | Partner
Guide | Partner

If statement for missing variable content

SOLVE

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