Reputation: 123
I am trying to loop over the articles in a blog to see if the article.tags contains any of the value in taggs where taggs is an array to be supplied by the user. I want to retrieve an array of related articles by tag.
Below is my example code:
{% assign taggs = "T-shirt,bag,Purse,Handbag" | split: "," %}
{%- assign length = b_s.related_article_blog_count | plus: 0 -%}
{% capture fourthtag %}{{ taggs[3] | downcase }}{% endcapture %}
<div class="jj-post-content">
<div class="item">
{%- for article in blogs[blog].articles limit: length -%}
{% if article.tags contains fourthtag %}
<a href="{{ article.url }}">
<div class="jj-title">{{ article.title }}</div>
</a>
{%- endif -%}
{%- endfor -%}
</div>
</div>
When I run the code it returns no result. But when I created the article tags in lowercase, I got all the articles filtered as expected. Given that many of the users may not be creating tags in lowercase, I need a way to downcase the article.tags
from the loop.
I tried {% capture atc %}{{ article.tags | downcase }}{% endcapture %}
before the loop but it was not working.
Upvotes: 0
Views: 490
Reputation: 123
I came up with this. Make a string, normalize the strings to remove as many oddities as possible, remake into an array to use with iteration.
{%- assign normalized_article_tags = article.tags | join:"," | downcase | split:"," %}
{% if normalized_article_tags contains fourthtag %}
You should also strip excess whitespace, end of lines, html and or handleize the tags to further normalize data being matches to potentially ambiguous user data.
Further
{% capture fourthtag %}{{ taggs[3] | downcase }}{% endcapture %}
can become
{% assign fourthtag = taggs[3] | downcase %}
Try to reserve using capture for outputs that will be repeated , such as snippets rendering , trying to do a kind of pseudo-interpolation, or long form text that would be obnxious to use with only an assign statement. https://shopify.dev/api/liquid/tags/variable-tags#capture
Upvotes: 1