Adding variables to blog data for output in for loop


#1

Hello,

I am wondering if it is possible to add variables to the blog data.

Currently to build out a blog listing page you would set a for loop

`{% for content in contents %}

{% endfor %}`

then you would call in variables that call in different parts of the blog posts

{{ content.foo ))

These variables are for elements already and always included in a hubspot blog post such as

author
date
title
summary
absolute link
etc.

But is it possible to add to these variables. For instance, lets say I add an image module into the blog post template. Is there a way to access this on the listing page?

What I am actually trying to do is allow the option to show any list item in one or two columns based on the choice module I set up on the blog post template. If when creating a blog post you choose 1 column or 2 column, a class in the wrapper inside the loop on the listing page will change accordingly.

the issue I am having is that while the choice module value is available to the listing page template (when in a hubl template module on the blog template, not in the blog post template), and the choice drop down is on the blog editing template and the choice can be made and saved, The listing page only recognizes the default choice and not the choice chosen in the blog editor.

half way through writing this I decided to move the choice module to the blog page template from the main blog template and now the variable for the choices is unavailable to the listing page. Keeping the choice module in the main blog template outside of the blog page template makes the variable for the choice available but not individually by the blogs. Because the choices chosen on the blogs aren’t saved per blog, only the default choice is available.

It would be great if there was a way to add it to the content variables so that you could do something like

{{ content.widget_data.foo.value }}

within the contents loop, making the individual choices of the blog posts available.

any ideas?


#2

I answered my own question.

I used the developer info (I had to use a code beautifier and the in page search) but I found the choice widget within the data for each post.

I was expecting it to be something like

{{ content.widget_data.foo.value }}

as this is how it works (minus content.) on a page, but it ended up being

{{ content.widgets.foo.body.value }}

and it is now outputting the choices made per post.

In case this is helpful to anyone else, I would like to mention that the choices module is actually placed in the blog page template (not the main blog template or the listing template).

This can work for any module, custom or otherwise. You just have to find it in the data and target accordingly.

my data looks like:

"contents": { "objects": [{ "widgets": { "columns": { "css": {}, "child_css": {}, "name": "columns", "type": "choice", "label": "Choose Columns", "smart_type": null, "id": "columns", "body": { "value": "col-wrap-2" } } },