How to assign value to an array in a for loop

SOLVE
Highlighted
New Contributor

I'm trying to make this work:

{% for topic in content.topic_list %}
    {% if topic.name!= 'SAMPLE_TOPIC'  %}
        
        {% set related[loop.index] = topic.name  %}
        
        <p>related: {{ related[loop.index] }}</p>

    {% endif %}
{% endfor %}

I need to create an array with some of topics (need to exclude some of them).

Any ideas how?

Thanks, Michael

Reply
0 Upvotes
2 Accepted solutions

Accepted Solutions
Top Advisor

@michalo,

 

Your question actually just led to a pretty cool discovery on my part. First I will show you:

 

I started with a sample dict:

% set test = {'1': 'one', '2': 'two', '3': 'three', '4': 'four', '6': 'six'} %}

I then tried to update the dict. HubL is a subset of Jinja2 which is a subset of python, and I have been learning some python here lately so  I was interested to see if this would work. It tools some poking and prodding but This:

{% set test2 = test.update({'7': 'seven'}) %}
{% set test2 = test.update({'8': 'eight'}) %}

made this:

{{ test }}

output this:

{1=one, 2=two, 3=three, 4=four, 6=six, 7=seven, 8=eight}

This is not documented in the HubL docs anywhere but it is an awesome little tid bit for sure.

 

Why this matters for you? Topics are stored as a dict as far as I can tell.  Look at how dicts work in python. Part of what gives it away for me with the topics is that they are not orderable which is a dict characteristic. 

From what I can tell topics operate from a main library (an interface for which you can find in your blogs dashboard). When blogs are called up the topics are scrolled through per blog and if a topic has been connected to that blog then the blog "borrows" that topic's information from the dictionary. 

 

Long story short, You can't inject data into the topics. You can however build your own dictionary by incorporating the method I described with what you have started. I haven't played with this with in a blog template with topics but running two consecutive updates on the same dict with both values retaining should mean that it can hold up in a forloop. All you need to do is structure your data to hold the topic name and the topic slug (for the url). 

 

I am going to play with this when I have a second, but if you get to it before I do let me know what what comes out of it.

Top Advisor

I couldn't resist nerding out over this so I ran a test on topics. I used a recent posts function. This works! I didn't apply any conditions so it just added all of the topics to my dictionary, but you can set your conditions and it will work.

{% set new_list = {} %}
{% set rec_posts = blog_recent_posts('default', 5) %}
{% for rec_post in rec_posts %}
    {% for topic in  rec_post.topic_list %}
        {% set new_list2 = new_list.update({"t_n": topic.name, "t_s": topic.slug}) %}
     {% endfor %}
      {{ new_list }}
{% endfor %}

You can create a list (basically python for array) as well:

{% set new_list = [] %}
{% set rec_posts = blog_recent_posts('default', 1) %}
{% for rec_post in rec_posts %}
    {% for topic in  rec_post.topic_list %}
        {% set new_list2 = new_list.append([topic.name, topic.slug]) %}
    {% endfor %}
    {{ new_list[1] }}
{% endfor %}

This is good if you want to access members by their index. You can apply splicing aswell but I can only get it to work with a start position and stop position. Steps aren't working and you can't leave the start or stop open ended.

 

Just notice that I declare the empty list/dict outside of the for loops. 

 

I hope this helps.

3 Replies
Top Advisor

@michalo,

 

Your question actually just led to a pretty cool discovery on my part. First I will show you:

 

I started with a sample dict:

% set test = {'1': 'one', '2': 'two', '3': 'three', '4': 'four', '6': 'six'} %}

I then tried to update the dict. HubL is a subset of Jinja2 which is a subset of python, and I have been learning some python here lately so  I was interested to see if this would work. It tools some poking and prodding but This:

{% set test2 = test.update({'7': 'seven'}) %}
{% set test2 = test.update({'8': 'eight'}) %}

made this:

{{ test }}

output this:

{1=one, 2=two, 3=three, 4=four, 6=six, 7=seven, 8=eight}

This is not documented in the HubL docs anywhere but it is an awesome little tid bit for sure.

 

Why this matters for you? Topics are stored as a dict as far as I can tell.  Look at how dicts work in python. Part of what gives it away for me with the topics is that they are not orderable which is a dict characteristic. 

From what I can tell topics operate from a main library (an interface for which you can find in your blogs dashboard). When blogs are called up the topics are scrolled through per blog and if a topic has been connected to that blog then the blog "borrows" that topic's information from the dictionary. 

 

Long story short, You can't inject data into the topics. You can however build your own dictionary by incorporating the method I described with what you have started. I haven't played with this with in a blog template with topics but running two consecutive updates on the same dict with both values retaining should mean that it can hold up in a forloop. All you need to do is structure your data to hold the topic name and the topic slug (for the url). 

 

I am going to play with this when I have a second, but if you get to it before I do let me know what what comes out of it.

Top Contributor

A similar way (with less extra variables) would be to use the do expression statement.

{% do test.update({ '9': 'nine', '10': 'ten' }) %}

 That way you don't have to keep setting new variables like test3, test4 etc...

 

The 'do' tag works exactly like the regular variable expression "{{ ... }}"; except it doesn’t print anything. This can be used to modify lists and dictionaries.
Top Advisor

I couldn't resist nerding out over this so I ran a test on topics. I used a recent posts function. This works! I didn't apply any conditions so it just added all of the topics to my dictionary, but you can set your conditions and it will work.

{% set new_list = {} %}
{% set rec_posts = blog_recent_posts('default', 5) %}
{% for rec_post in rec_posts %}
    {% for topic in  rec_post.topic_list %}
        {% set new_list2 = new_list.update({"t_n": topic.name, "t_s": topic.slug}) %}
     {% endfor %}
      {{ new_list }}
{% endfor %}

You can create a list (basically python for array) as well:

{% set new_list = [] %}
{% set rec_posts = blog_recent_posts('default', 1) %}
{% for rec_post in rec_posts %}
    {% for topic in  rec_post.topic_list %}
        {% set new_list2 = new_list.append([topic.name, topic.slug]) %}
    {% endfor %}
    {{ new_list[1] }}
{% endfor %}

This is good if you want to access members by their index. You can apply splicing aswell but I can only get it to work with a start position and stop position. Steps aren't working and you can't leave the start or stop open ended.

 

Just notice that I declare the empty list/dict outside of the for loops. 

 

I hope this helps.