custom module - choose the blog

SOLVE
Highlighted
Regular Contributor

Hi, I'm creating a custom module to have a slider post.
I need to choose the blog from a list and have the number of posts to display. The syntax to set the blog is this: {% set posts = blog_recent_posts('default', 5) %} but how do I make dynamic the field "default" = blog name or its id, and the field "5" = number of posts to be displayed?

I've tried with this sintax

{% set posts = blog_recent_posts('{{widget.blog_name}}', '{{widget.n_posts}}') %}

but do not work

Can anyone help me?

Many thanks

 

Giovanni

Reply
0 Upvotes
1 Accepted solution

Accepted Solutions
HubSpot Employee

Hi Gio,

 

When you are within a HubL statement, it is not necessary to wrap HubL tokens in expression delimiters

 

{{ }}

 

 Making these slight changes to your code should get this working

 

{% text "blog_id" label="Blog ID", value="4495212459", export_to_template_context=True %}
{% set post_num = [1, 2, 3, 4, 5, 6] %}
{% choice "blog_limit" label="Number of Posts", value="5", choices="post_num", export_to_template_context=True %}
{% set posts = blog_recent_posts(widget_data.blog_id.value, widget_data.blog_limit.value) %}

 

Best,

Danielle

7 Replies 7
Esteemed Advisor

How are you wanting to edit this? from the page editor?

 

You could create editable fields: 

 

 

{% text "blog_id" label="Blog ID", value="12345678", export_to_template_context=True %}

{% set post_num = [1, 2, 3, 4, 5, 6] %}
{% choice "blog_limit" label="Number of Posts", value="5", choices="{{ post_num }}", export_to_template_context=True %}

 

Then assign the values to the blog search funtion:

 

 

{% set posts = blog_recent_posts('{{ widget_data.blog_id.value }}', '{{ widget_data.blog_limit.value }}') %}

 

this would create editable fields on the editing page. You would put the blog ide in the text field, and use the choice dropdown to choose the blog limit. 

 

Let me know if this is what you need and if it works.

 

- Jonathan Sumner
Reply
0 Upvotes
Regular Contributor

yes this is exactly what I need, but unfortunately does not work and I do not understand why

Reply
0 Upvotes
Esteemed Advisor
Will you paste your code here so i can take a look?
- Jonathan Sumner
Reply
0 Upvotes
Regular Contributor

Thanks for help me

---------

{% text "blog_id" label="Blog ID", value="4495212459", export_to_template_context=True %}
{% set post_num = [1, 2, 3, 4, 5, 6] %}
{% choice "blog_limit" label="Number of Posts", value="5", choices="{{ post_num }}", export_to_template_context=True %}

{% set posts = blog_recent_posts('{{ widget_data.blog_id.value }}', '{{ widget_data.blog_limit.value }}') %}

<p>{{ widget_data.blog_id.value }}</p>

----------

the last row is for test

 

Gio(vanni)

 

Reply
0 Upvotes
HubSpot Employee

Hi Gio,

 

When you are within a HubL statement, it is not necessary to wrap HubL tokens in expression delimiters

 

{{ }}

 

 Making these slight changes to your code should get this working

 

{% text "blog_id" label="Blog ID", value="4495212459", export_to_template_context=True %}
{% set post_num = [1, 2, 3, 4, 5, 6] %}
{% choice "blog_limit" label="Number of Posts", value="5", choices="post_num", export_to_template_context=True %}
{% set posts = blog_recent_posts(widget_data.blog_id.value, widget_data.blog_limit.value) %}

 

Best,

Danielle

Esteemed Advisor
Your creating a array. To get the data from it you have to loop through it with a for loop. {% for post in posts %}{% endfor %}

Then you access the data by targeting post. {{ post.name }} post being the single item name in the for loop.
- Jonathan Sumner
Reply
0 Upvotes
Regular Contributor

Ok this is very strange, because inside a custom modul the script don't work instead directly inside hubl module this work fine!!! Yeah

Thanks a lot 

 

Gio(vanni)

Reply
0 Upvotes