Hubl filter that perform the same function with Javascript include method


#1

Hi,

I am new to HubL.

I am trying to find a way to check if a value exist in an array. I am looking for a filter that work the same with JS ES6 “include” method

For Example:

{% set x = post.tag_list %}
{% set y = ‘article’ %}
{% if x include y %} //return true
//do something
{% else %}
//do something
{% endif %}

I’ve tried using ‘in’ did not worked
{% if ‘article’ in post.tag_list %}
{{ do something }}
{% elif loop.index == 1 %}
{{ do something }}
{% endif %}

or

if there is another solution to check whether a tag exist within a blog tag_list and if the tag did not exist within blog tag_list return the first blog in the loop.

If anyone could help me with this it would be very helpful.

Thanks before.


#2

Hi @acaroles,

It sounds like you might be able to use the select filter, which filters a sequence of objects by applying a test to the object and only selecting the ones with the test succeeding.


#3

Hi Derek,

I’ve tried using select as per your info but I could not find an example of how to check a specific value within a sequence.

The documentation only provide example of using expression. When I perform a select filter based on a specific value, it keep telling me no expression defined or cannot resolve property.

{% content.tag_list|select(“article”) %} --> no expression defined error
{% content.tag_list|select(“equalto”) %} --> at least 1 parameter required error but no explanation where to put this parameter within the select filter.

{% content.tag_list|selectattr(“slug”,“article”) %} --> cannot resolve property ‘article’ in ‘article’ error ???

Could you give an example of how to do this since the documentation is not clear enough on performing specific value search within a sequence ?

Thank you for your help.


#4

Hi @acaroles,

This appears to be an issue with the way the select() function works (see below). I’ve filed a github issue that we can monitor, and on it’s resolution you should be able to use the equalto test and select filter together.