CMS Development

pdaniel
Member

Capture topics from widget module input

SOLVE

I have a module being printed to template (input by a user) and would like to be able to capture the topics from this widget module - and use it to display related posted underneath - there doesn't seem to be any way to capture this that I can see - other than referencing it directly - but then it won't dynamically change if the user changes it

 

- any help greatly appreciated

 

0 Upvotes
1 Accepted solution
Jsum
Solution
Key Advisor

Capture topics from widget module input

SOLVE

@pdaniel,

 

I am not entirely sure that I follow your question. You have a module that your user sets up in the page editor? and they place or choose a topic inside this module? And you want to show blog posts related to this topic on the page?

 

If this is what you are wanting to do then I think I have a pretty solid solution for you, though a bit complicated and it will require custom code (as apposed to drag and drop templates).

 

So the first thing you are going to want to do is allow a topic to be input. You could do this with a text module, which would allow the user to type the topic in, but that allows for errors such as case sensitivity and mispellings so it isn't a great solution. The second option is to create an array containing the topics and use this as option in a drop down. This is better because it would allow the user to simply pick a topic from a dropdown. But you would have to update the array with new topics as you add them to your blog and you would still be responsible for spelling and casing. 

 

The best solution for this would be to dynamically generate the array by first grabing all of the topics in your blog, then setting these topics to a dropdown like this:

 

{% set my_topics = blog_topics('default', 250) %}

This creates your topics array, where "default" is the blog name (default for only or default blog) and 250 is the literal maximum amount of topics allowed to return. You can lower this number if you would like or keep it at max. raising it will do nothing.

 

now the choice module:

 

{% choice "topic_selector" label="Select a Topic", value="", choices="{{ my_topics }}", export_to_template_context=True %}

Note that I am using "export_to_template_context=True"

 

 

This will create a drop down in your page editor that contains all of the topics of the blog you selected. Now you just need an feed that filters by the selected topic:

 

{% set topic_posts = blog_recent_topic_posts('default', widget_data.topic_selector.value, 5) %}
{% for topic_post in topic_posts %}
    <div class="post-title">{{ topic_post.name }}</div>
{% endfor %}

This first creates an array of posts from the "default" blog (like above), but the posts have to have the topic specified by your choice module (widget_data.topic_sector.value is a data token that represents the choice made in the choice module), and 5 is the limit. You can change the to 1-100+ to return the desired amount of posts.

 

Now all you have to do is loop through the results. thats what the for loop is for. Notice "topic_post.name" inside the for loop. all of the blog loop data tokens ( .name, .absolute_url, etc.) will work here but instead of using content.name or etc, you would use the item name in the for loop, i.e topic_post.name.

 

 

 

 

 

View solution in original post

0 Upvotes
2 Replies 2
Jsum
Solution
Key Advisor

Capture topics from widget module input

SOLVE

@pdaniel,

 

I am not entirely sure that I follow your question. You have a module that your user sets up in the page editor? and they place or choose a topic inside this module? And you want to show blog posts related to this topic on the page?

 

If this is what you are wanting to do then I think I have a pretty solid solution for you, though a bit complicated and it will require custom code (as apposed to drag and drop templates).

 

So the first thing you are going to want to do is allow a topic to be input. You could do this with a text module, which would allow the user to type the topic in, but that allows for errors such as case sensitivity and mispellings so it isn't a great solution. The second option is to create an array containing the topics and use this as option in a drop down. This is better because it would allow the user to simply pick a topic from a dropdown. But you would have to update the array with new topics as you add them to your blog and you would still be responsible for spelling and casing. 

 

The best solution for this would be to dynamically generate the array by first grabing all of the topics in your blog, then setting these topics to a dropdown like this:

 

{% set my_topics = blog_topics('default', 250) %}

This creates your topics array, where "default" is the blog name (default for only or default blog) and 250 is the literal maximum amount of topics allowed to return. You can lower this number if you would like or keep it at max. raising it will do nothing.

 

now the choice module:

 

{% choice "topic_selector" label="Select a Topic", value="", choices="{{ my_topics }}", export_to_template_context=True %}

Note that I am using "export_to_template_context=True"

 

 

This will create a drop down in your page editor that contains all of the topics of the blog you selected. Now you just need an feed that filters by the selected topic:

 

{% set topic_posts = blog_recent_topic_posts('default', widget_data.topic_selector.value, 5) %}
{% for topic_post in topic_posts %}
    <div class="post-title">{{ topic_post.name }}</div>
{% endfor %}

This first creates an array of posts from the "default" blog (like above), but the posts have to have the topic specified by your choice module (widget_data.topic_sector.value is a data token that represents the choice made in the choice module), and 5 is the limit. You can change the to 1-100+ to return the desired amount of posts.

 

Now all you have to do is loop through the results. thats what the for loop is for. Notice "topic_post.name" inside the for loop. all of the blog loop data tokens ( .name, .absolute_url, etc.) will work here but instead of using content.name or etc, you would use the item name in the for loop, i.e topic_post.name.

 

 

 

 

 

0 Upvotes
pdaniel
Member

Capture topics from widget module input

SOLVE

Hi Jsum,

Thanks so much for detailing a great solution - and apologies for the late reply and really appreciate your time. I'm new to Hubspot development and the community - and will endevour to be more concised with questions in future - but you nailed it - cheers!

0 Upvotes