CMS Development

pandapandapanda
Participante

Looping through an array, if matched, then change a variable

I have this piece of code that has left me scratching my head for a couple of days. 

 

I'm trying to get the URL of the current page, and if it matches one of the values in the  ignoredPaths array, it changes the variable isInIgnoredPaths to 'yes'

 

{% set urlPath = request.path %}
{% set isInIgnoredPaths = "no" %}
{% set ignoredPaths = ["/au/legal", "/au/careers", "/au/investor"] %}
{% for pathRow in ignoredPaths %}
{% if pathRow is string_containing urlPath %}
{% set isInIgnoredPaths = "yes" %}
{% endif %}
{% endfor %}

I've tried a few iterations of this code and I keep getting 'no'. I even went to the trouble of rebuilding this code in Javascript just to prove I wasn't losing my mind. The same logic works fine in JS.

0 Avaliação positiva
1 Resposta 1
lscanlan
Alunos da HubSpot
Alunos da HubSpot

Looping through an array, if matched, then change a variable

Hi @pandapandapanda,

 

So I'll point out a few things here.

 

First, the string_containing expression test _will_ work, but I don't know if it will work in the way you're expecting it to. Generally though the 1st value will be a larger string, and the is_containing expression test will check to see if the 2nd value is contained within the 1st value. So in your case, you may want to reverse those values. So instead of:

 

{% if pathRow is string_containing urlPath %}

...you'd instead want:

 

{% if urlPath is string_containing pathRow %}

This way if you have an example URL of https://www.domain.com/au/careers/job-listing, an expression test of {% if urlPath is string_containing pathRow %} will evaluate to true, because the pathRow (in this example /au/legal )is contained within urlPath (in this example /au/careers/job-listing ).

 

And second, it's important to remember that variables in HubL are loop-scoped. I think sometimes it's helpful to print out the variables in HTML. So take a look at what this code prints out:

 

{% set myBoolean = false %}
{% set myArray = [1, 2, 3] %}
{% for number in myArray %}
  {% set myBoolean = true %}
  <p>(inside loop) myBoolean: {{ myBoolean }}</p>
{% endfor %}
<p>(outside loop) myBoolean: {{ myBoolean }}</p>

Inside the loop we're setting myBoolean to true. And so for each loop iteration, we'll see it printed out as true. And then outside the loop, because the variable is loop-scoped, it becomes reset to false. So if you're trying to access the value of your isInIgnoredPaths outside of your for loop, it will be set to "no", because the "yes" value is only stored within the context of the for loop. So usually what I'll recommend is to run your code inside the loop. And you can even do it at the end of the last loop iteration. So in your case, that might look something like this:

 

{% set urlPath = request.path %}
{% set isInIgnoredPaths = "no" %}
{% set ignoredPaths = ["/au/legal", "/au/careers", "/au/investor"] %}

{% for pathRow in ignoredPaths %}
  {% if urlPath is string_containing pathRow %}
    {% set isInIgnoredPaths = "yes" %}
  {% endif %}
  {% if loop.last and isInIgnoredPaths == yes %}

    {# your code here #}

  {% endif %}
{% endfor %}

I hope that helps. Let me know if you have questions about any of that.

 

Leland Scanlan

HubSpot Developer Support
0 Avaliação positiva