Mar 13, 201711:34 AM - edited Mar 13, 201711:35 AM
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 %}
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.
@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]
{% 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 %}
@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.
{% 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.
Mar 13, 201711:34 AM - edited Mar 13, 201711:35 AM
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 %}