CMS Development

peesen
Contributor

Show values of image with "export_to_template_context=True"

SOLVE

Hi HubSpot-Community

 

In my single-blog template i use "export_to_template_context=True" for an image tag. I need to use this image inside the blog loop to use this as the preview image. How can i access the url and alt values of this tag?

 

{% image "preview_image" label='Preview Image', src='//cdn2.hubspot.net/hub/53/file-733888619-jpg/assets/hubspot.com/about/management/brian-home.jpg', export_to_template_context=True %}

 

Thank you so much for your help!

Pascal

 

0 Upvotes
1 Accepted solution
stefen
Solution
Key Advisor | Partner
Key Advisor | Partner

Show values of image with "export_to_template_context=True"

SOLVE

@peesen it sounds like you have a module on a blog post with a custom field that you're trying to retrieve inside the blog listing loop?

 

If that's the case, you need to reference by using the "widgets" syntax like so:

 

{% for content in contents %}
<img src="{{ content.widgets.preview_image.body.src }}">
{% endfor %}

 

 More info here: https://developers.hubspot.com/docs/cms/building-blocks/modules/export-to-template-context#printing-...

Stefen Phelps, Community Champion, Kelp Web Developer

View solution in original post

5 Replies 5
Anton
Recognized Expert | Diamond Partner
Recognized Expert | Diamond Partner

Show values of image with "export_to_template_context=True"

SOLVE
0 Upvotes
peesen
Contributor

Show values of image with "export_to_template_context=True"

SOLVE

Hi @Anton 

 

Thank you for your help. Sorry i dont really get it. The default modules i understand perfectly. But how can i access a export_to_template_contect value in a script? I cant really find that information in the documentary.

 

Thank you for your help and best regars.

 

 

0 Upvotes
stefen
Solution
Key Advisor | Partner
Key Advisor | Partner

Show values of image with "export_to_template_context=True"

SOLVE

@peesen it sounds like you have a module on a blog post with a custom field that you're trying to retrieve inside the blog listing loop?

 

If that's the case, you need to reference by using the "widgets" syntax like so:

 

{% for content in contents %}
<img src="{{ content.widgets.preview_image.body.src }}">
{% endfor %}

 

 More info here: https://developers.hubspot.com/docs/cms/building-blocks/modules/export-to-template-context#printing-...

Stefen Phelps, Community Champion, Kelp Web Developer
jau
Contributor | Platinum Partner
Contributor | Platinum Partner

Show values of image with "export_to_template_context=True"

SOLVE

S,

 

EDIT: I realized you guys were speaking in reference to blog listing pages. My situation is a typical lp or webpage template. Apologies, didn't mean to hijack this thread. However if you do have any thoughts on my problem id appreciate any input.

 

Can you confirm that your solution does these two things, I ask because I've have reocurring issues with this functionality and support hasn't provided a clear answer.

 

1. Set a default image for the image module when a page is built using a template with this code on it. 

2. That image be editable on the page level.

 

The issue i've run into over and over again is calling the value of the image module. The problem I run into is in order to have the default image load as a background image url I use the call: 

 

{{ widget_data.background_image.src }}  

 

Where background_image is the name of the module. This loads my default src value set in the code great. But once a page is built and someone uses that module to pick a new image it does not load the new image unless the code is more specific like this:

 

{{ widget_data.background_image.img.src }}

 

But having that value in the code breaks the usage of the default image src being set. So what im running into is i basically cannot set a default value for the image module AND it actually be editable by the end user. 

 

I've previously referenced this documentation:

https://developers.hubspot.com/docs/cms/building-blocks/modules/export-to-template-context

 

And support told me the example code is not intended to be used "as is". Explaining why it doesn't work.

 

Thanks for any potential help

0 Upvotes
Daniel_eon
Participant

Show values of image with "export_to_template_context=True"

SOLVE

Hi jau,

 

I know your question is over a year old, but I ran into the exact same problem (on a blog post template, though, but this should make no difference). For anyone reading this, I solved it with a simple if...else statement.

background-image: url('{% if not widget_data.blogpost_header_image.img.src %}{{ widget_data.blogpost_header_image.src }}{% else %}{{ widget_data.blogpost_header_image.img.src }}{% endif %}');"

 

0 Upvotes