CMS Development

John
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

Show if content has matching string(s) in sequence

SOLVE

As of now, this only works when content.name has everything in the sequence defined below. 

 

For example, if the content's name was "lorem dolor is an ipsum," then it would work.

If it was "lorem said ipsum," then it would not print out a value (because it is missing "dolor").

{# ***********  VARIABLES ************ #}
{% set sequence_value = ['lorem', 'ipsum', 'dolor'] %}
{% set content_value = content.name %}

{# ************** CODE *************** #}
{% if content_value is string_containing sequence_value %}
          <div>foo</div>
{% endif %}

Hopefully this post will help someone else out in the future as well.



I like kudos almost as much as cake – a close second.

0 Upvotes
1 Accepted solution
Jsum
Solution
Key Advisor

Show if content has matching string(s) in sequence

SOLVE

@John,

 

You will need to loop through "sequence_value" and check each item against your "content_value".

 

{# ***********  VARIABLES ************ #}
{% set sequence_value = ['lorem', 'ipsum', 'dolor'] %}

{# ************** CODE *************** #}
{% for content in contents %}

    {% for value in sequence_value %}

        {% if value in content.name %}

             {# OUTPUT #}

        {% endif %}

    {% endfor %}
{% endfor %}

I threw in the contents loop as I assumed you would need this applied. You can omit if needed.

 

Bare in mind that this will loop through each content and for each index of your content will loop through your sequence_value. If either or both set is relatively small then it won't be an issue but if both are large then it could add to your load time. 

 

*EDIT: I removed the content.name variable as it seamed redundant. Your var name was longer than the acutal token name and the var was declared outside of the contents for loop.

View solution in original post

3 Replies 3
Jsum
Solution
Key Advisor

Show if content has matching string(s) in sequence

SOLVE

@John,

 

You will need to loop through "sequence_value" and check each item against your "content_value".

 

{# ***********  VARIABLES ************ #}
{% set sequence_value = ['lorem', 'ipsum', 'dolor'] %}

{# ************** CODE *************** #}
{% for content in contents %}

    {% for value in sequence_value %}

        {% if value in content.name %}

             {# OUTPUT #}

        {% endif %}

    {% endfor %}
{% endfor %}

I threw in the contents loop as I assumed you would need this applied. You can omit if needed.

 

Bare in mind that this will loop through each content and for each index of your content will loop through your sequence_value. If either or both set is relatively small then it won't be an issue but if both are large then it could add to your load time. 

 

*EDIT: I removed the content.name variable as it seamed redundant. Your var name was longer than the acutal token name and the var was declared outside of the contents for loop.

John
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

Show if content has matching string(s) in sequence

SOLVE

Thank you @Jsum, nice touch on the contents loop. You were right.

 

I do wonder what you'd consider too large / performance-hindering.

The sequence value will be a search that is stripped to a sequence of 7 values (duplicates also removed).

{% set sequence_value = request.query_dict.search|lower|split(' ', 7)|unique %}

 

Using this technique, would indexing each blog post's title be poor practice on a large blog?

To take it a step further, each blog post's entire content?



I like kudos almost as much as cake – a close second.

0 Upvotes
Jsum
Key Advisor

Show if content has matching string(s) in sequence

SOLVE

@John,

 

I am by no means an expert in efficiency, nor have I experience your exact circumstances so I couild not say whether it would be a hindrance or not. 

 

The way I see it though is that you have to loop through the contents anyways so all you are doing is adding a loop with 7 iterations to each iteration of the contents loop. The if statement inside the nested loop will cause the server to read the content.name per contents iteration. If you add the full blog content to be checked it would have to read all of the content per contents iteration. Either way this is done on the server side and, in my experience, Hubspot's servers have no problem processing a lot of logic quickly. I don't believe it will be an issue with just 7 nested iterations. All you can really do is set it up and check to ensure the page doesn't take much longer to load than it did originally. 

0 Upvotes