CMS Development

stefen
Key Advisor | Partner
Key Advisor | Partner

topic_list if in statement

SOLVE

Can anyone tell me why this if statement isn't working?

{% set myString = "Example" %}

{% if myString|lower in content.topic_list|lower %}
blah
{% endif %}

This exact if statement works with content.name and content.post_summary but it doesn't like the topic array for some reason... 

 

Here is the page for reference - click on the "search by topic"

 

help me @Jsum you're my only hope! Smiley Happy

Stefen Phelps, Community Champion, Kelp Web Developer
0 Upvotes
1 Accepted solution
stefen
Solution
Key Advisor | Partner
Key Advisor | Partner

topic_list if in statement

SOLVE

@Jsum the only thing I can think of is if statements do not work with arrays. I changed my code so the if statement is inside the topic loop and it now works perfectly:

{% set blog_1 = blog_recent_posts('111', limit=50) %}
{% set blog_2 = blog_recent_posts('222', limit=50) %}
{% set blog_3 = blog_recent_posts('333', limit=50) %}
{% set blog_4 = blog_recent_posts('444', limit=50) %}

{% set all_posts = (blog_1 + blog_2 + blog_3 + blog_4) | sort(true, false, 'publish_date')  %}

{% set myString = request.query_dict['title'] %}

{% for all_post in all_posts %}
    {% if myString %}
        {% for topic in all_post.topic_list %}
            {% if myString|lower in topic.name|lower %}
                this returns posts with this topic. hooray!
            {% endif %}
        {% endfor %}
    {% endif %}
{% endfor %}

 Smiley Very Happy

Stefen Phelps, Community Champion, Kelp Web Developer

View solution in original post

0 Upvotes
7 Replies 7
Jsum
Key Advisor

topic_list if in statement

SOLVE

hey @stefen you flatter me. 

 

have you tried using the map filter on the topic_list?

 

content.topic_list|map('name')

I might be incorrect but what I think the deal is that topic_list is actually an object itself, witch multiple (though few) attributes with name being one. So your if statement doesn't know what part of topic_list to be checking. 

stefen
Key Advisor | Partner
Key Advisor | Partner

topic_list if in statement

SOLVE

@Jsum When printing it to the page both {{ content.topic_list }} and {{ content.topic_list|map('name') }} print the same thing which just looks like a normal array e.g. [topic one, topic two, topic 3]

 

¯\_(ツ)_/¯

Stefen Phelps, Community Champion, Kelp Web Developer
0 Upvotes
Jsum
Key Advisor

topic_list if in statement

SOLVE

@stefen well this works

 

{% for content in contents %}
            
              {% set myString = "featured" %}
            
                        {% if myString in content.topic_list|map('name') %}
                            
                            blah
                            
                        {% else %}
                            
                            nope
                                
                        {% endif %}
                    
                {% endfor %}

if the blog has a topic of "Featured" it would pint blah, and print nope if not. 

 

Using |lower on the topic list caused all nopes.

 

Using the topics list without |map('name') printed all nopes.

 

again I think topics have a system name (i.e. brown_trucks) and a display name (i.e. Brown Trucks). with out differentiating between the two it just confuses your code. I am not 100% on this still. 

 

I'm not sure why converting the "mystring" variable and the topics list to lower case didn't work, but it doesn't. You can convert just the 'mystring' variable to lower case and it would still work but that would be redundant because it seams to work without it. 

 

 

EDIT*****

 

You need to convert to lower due to case sensitivity. Luckily this does work, I just tested it wrong before:

 

{% for content in contents %}
            
                    {{ loop.index }}
                    {% set myString = "Featured" %}
            
                        {% if myString|lower in content.topic_list|map('name')|lower %}
                            
                            blah
                            
                        {% else %}
                            
                            nope
                                
                        {% endif %}
                    
                {% endfor %}

 

stefen
Key Advisor | Partner
Key Advisor | Partner

topic_list if in statement

SOLVE

@Jsum thanks for the help. But I must be overlooking something... it still isn't working for me. But I'm also using a custom loop but not sure why that would be related to this.  Here is the context I'm using it in:

{% set blog_1 = blog_recent_posts('111', limit=50) %}
{% set blog_2 = blog_recent_posts('222', limit=50) %}
{% set blog_3 = blog_recent_posts('333', limit=50) %}
{% set blog_4 = blog_recent_posts('444', limit=50) %}

{% set all_posts = (blog_1 + blog_2 + blog_3 + blog_4) | sort(true, false, 'publish_date')  %}

{% set myString = request.query_dict['title'] %}

{% for all_post in all_posts %}
    {% if myString and myString|lower in all_post.name|lower %}
        this will find the string in the post name
    {% elif myString and myString|lower in all_post.topic_list|map('name')|lower %}
        this doesn't find the string in the topic
    {% elif myString and myString|lower in all_post.post_summary %}
        this will find the string in the summary
    {% endif %}
{% endfor %}

I'm first checking to see if the string exists so it doesn't return all posts when it's empty.

 

Reference URL using the "|map('name')|lower" filters and still not working: http://www.quadcitysafety.com/blog/search?title=Spill

 

Smiley Frustrated

Stefen Phelps, Community Champion, Kelp Web Developer
0 Upvotes
Jsum
Key Advisor

topic_list if in statement

SOLVE

@stefen I did this:

 

{% set myString = request.query_dict['featured'] %}
                 
                {% for content in contents %}
                    {% if myString|lower in content.name|lower %}
                    
                        {{ loop.index }}. {{ content.name }}<br>
                        
                    {% elif myString|lower in content.topic_list|map('name')|lower %}
                    
                        this doesn't find the string in the topic
                        
                    {% elif myString|lower in content.post_summary %}
                    
                        this will find the string in the summary
                        
                    {% endif %}
                {% endfor %}

I removed the and part of the statements because it doesn't seam like you should be checking if the regular case AND the lower case version both match the lower case version of what you are checking, and yours didn't work with it (i'm not in your context though).

 

Secondly, I noticed that the only statement that worked was the first. The second two did not. The first, that worked, doesn't just filter by blog name though. Even though you are searching for the query in the content.name, it actually outputs all blogs that have that query in the name, topics, or summary. I Specifically checked to make sure that I had one of each:

 

1. a blog with "featured" in the title only

2. a blog with "featured" as a topic only

3. a blog with "featured" in the content only

 

All of these blogs came in off of the content.name condition alone so I changed it:

{% set myString = request.query_dict['featured'] %}
                 
                {% for content in contents %}
                    {% if myString|lower in content.post_body|lower %}
                    
                        {{ loop.index }}. {{ content.name }}<br>
                        
                    {% endif %}
                {% endfor %}

and this works the same. 

Hopefully this helps. I haven't used request.query_dict before so I am not familiar with exactly how it works, but I broke it and fixed it and what I put above worked in as far as I understand what you are needing, though it isn't as precise as you are trying to target. 

stefen
Solution
Key Advisor | Partner
Key Advisor | Partner

topic_list if in statement

SOLVE

@Jsum the only thing I can think of is if statements do not work with arrays. I changed my code so the if statement is inside the topic loop and it now works perfectly:

{% set blog_1 = blog_recent_posts('111', limit=50) %}
{% set blog_2 = blog_recent_posts('222', limit=50) %}
{% set blog_3 = blog_recent_posts('333', limit=50) %}
{% set blog_4 = blog_recent_posts('444', limit=50) %}

{% set all_posts = (blog_1 + blog_2 + blog_3 + blog_4) | sort(true, false, 'publish_date')  %}

{% set myString = request.query_dict['title'] %}

{% for all_post in all_posts %}
    {% if myString %}
        {% for topic in all_post.topic_list %}
            {% if myString|lower in topic.name|lower %}
                this returns posts with this topic. hooray!
            {% endif %}
        {% endfor %}
    {% endif %}
{% endfor %}

 Smiley Very Happy

Stefen Phelps, Community Champion, Kelp Web Developer
0 Upvotes
Jsum
Key Advisor

topic_list if in statement

SOLVE

@stefen Good stuff man. I will have to remember that.

0 Upvotes