Share Your Work

Fabinol
Participant

Setting variable of HubL function inside a template when creating content

SOLVE

Hello all

 

I am creating an email template for our company which updates subscribers on recent activity from our blog. I'm using HubL functions (as opposed to the RSS module) in order combine my own markup and apply inline CSS styles.

 

The following is a simplified version of the function:

{% set rec_posts = blog_recent_posts('default', 5 ) %}
{% for rec_post in rec_posts %}
    <span><a href="{{ rec_post.url }}">{{ rec_post.name }}</a></span><br>
    <span>{{ rec_post.blog_post_author }}</span><br>
    <span>{{ rec_post.publish_date_localized }}</span><br>
    <span>{{ rec_post.post_summary }}</span><br>
    <img src="{{ rec_post.featured_image }}" width="200" alt="{{ rec_post.featured_image_alt_text }}" /></span><br>
    <hr>
{% endfor %}

 

What I am looking to do is be able to set the variable that defines how many blog posts get listed on the Email Level as opposed to the template level. So each time we create an email using a template with the above function, we can change the number of posts that get displayed.

 

For example change:

{% set rec_posts = blog_recent_posts('default', 5 ) %}

to

{% set rec_posts = blog_recent_posts('default', 2 ) %}

if sending an email whereby we only want to show the last two posts from our blog.

 

Basically I would like to replicate the functionality of Hubspot's built-in RSS feed module which lets you set the number of posts displayed but have control over the markup and styling and have it as part of the template.

 

Any help is much appreciated.

 

0 Upvotes
1 Accepted solution
DaniellePeters
Solution
Top Contributor

Setting variable of HubL function inside a template when creating content

SOLVE

You could try something like adding a hidden choice module so that the user can select the value and then using the token for that value in the blog_recent_posts function like so:

{% choice "number_of_posts" label='Choose the number of posts', value='5' choices='1, 2, 3, 4, 5', export_to_template_context=True %}
{% set rec_posts = blog_recent_posts('default', widget_data.number_of_posts.value ) %}
{% for rec_post in rec_posts %}
    <span><a href="{{ rec_post.url }}">{{ rec_post.name }}</a></span><br>
    <span>{{ rec_post.blog_post_author }}</span><br>
    <span>{{ rec_post.publish_date_localized }}</span><br>
    <span>{{ rec_post.post_summary }}</span><br>
    <img src="{{ rec_post.featured_image }}" width="200" alt="{{ rec_post.featured_image_alt_text }}" /></span><br>
    <hr>
{% endfor %}

 http://designers.hubspot.com/docs/hubl/hubl-supported-modules#choice

View solution in original post

2 Replies 2
DaniellePeters
Solution
Top Contributor

Setting variable of HubL function inside a template when creating content

SOLVE

You could try something like adding a hidden choice module so that the user can select the value and then using the token for that value in the blog_recent_posts function like so:

{% choice "number_of_posts" label='Choose the number of posts', value='5' choices='1, 2, 3, 4, 5', export_to_template_context=True %}
{% set rec_posts = blog_recent_posts('default', widget_data.number_of_posts.value ) %}
{% for rec_post in rec_posts %}
    <span><a href="{{ rec_post.url }}">{{ rec_post.name }}</a></span><br>
    <span>{{ rec_post.blog_post_author }}</span><br>
    <span>{{ rec_post.publish_date_localized }}</span><br>
    <span>{{ rec_post.post_summary }}</span><br>
    <img src="{{ rec_post.featured_image }}" width="200" alt="{{ rec_post.featured_image_alt_text }}" /></span><br>
    <hr>
{% endfor %}

 http://designers.hubspot.com/docs/hubl/hubl-supported-modules#choice

Fabinol
Participant

Setting variable of HubL function inside a template when creating content

SOLVE

Thanks Danielle, that works like a charm! Smiley Very Happy

0 Upvotes