CMS Development

Greg_Batenburg
Participante

How to output 2nd indexed item in a list?

resolver

 

I want to output the 2nd item in a list.  For example, I'd like to output "oranges" from my list below, but this is not printing anything at all:

 

{% set items= ['apples, 'oranges', 'bananas', 'pears'] %}

{% for item in items %}
  {% if loop.index == 2 %}
     {% set output = item %}
{{ output|pprint }} {% endif %} {% endfor %}

Any ideas what the best way is to do this?

 

0 Avaliação positiva
1 Solução aceita
Gonzalo
Solução
Top colaborador(a) | Parceiro Diamante
Top colaborador(a) | Parceiro Diamante

How to output 2nd indexed item in a list?

resolver

Hey @Greg_Batenburg

 

You have a missing quote to close the first word apples.
Fixed code:

{% set items= ['apples', 'oranges', 'bananas', 'pears'] %}

{% for item in items %}
  {% if loop.index == 2 %}
     {% set output = item %}
     {{ output|pprint }}
  {% endif %}
{% endfor %}

Also, to print just the value you don't need the |pprint filter. Its used to print a whole array and the type of the function and things like that while debuggin. In this case the |pprint filter will print:
(String: oranges)

instead only:
orange

 

Regards,

Gonzalo

If this answer helps you to solve your questions please mark it as a solution.

Thank you,


Gonzalo Torreras

HubSpot freelance developer

hola@gonzalotorreras.com
www.gonzalotorreras.com
Schedule a meeting

Exibir solução no post original

0 Avaliação positiva
3 Respostas 3
Gonzalo
Solução
Top colaborador(a) | Parceiro Diamante
Top colaborador(a) | Parceiro Diamante

How to output 2nd indexed item in a list?

resolver

Hey @Greg_Batenburg

 

You have a missing quote to close the first word apples.
Fixed code:

{% set items= ['apples', 'oranges', 'bananas', 'pears'] %}

{% for item in items %}
  {% if loop.index == 2 %}
     {% set output = item %}
     {{ output|pprint }}
  {% endif %}
{% endfor %}

Also, to print just the value you don't need the |pprint filter. Its used to print a whole array and the type of the function and things like that while debuggin. In this case the |pprint filter will print:
(String: oranges)

instead only:
orange

 

Regards,

Gonzalo

If this answer helps you to solve your questions please mark it as a solution.

Thank you,


Gonzalo Torreras

HubSpot freelance developer

hola@gonzalotorreras.com
www.gonzalotorreras.com
Schedule a meeting
0 Avaliação positiva
Greg_Batenburg
Participante

How to output 2nd indexed item in a list?

resolver

Thanks!  I also learned my lesson that I am not allowed to pass variables out of the for loop in HUBL.

 

 

0 Avaliação positiva
Gonzalo
Top colaborador(a) | Parceiro Diamante
Top colaborador(a) | Parceiro Diamante

How to output 2nd indexed item in a list?

resolver

Right!

If you need to "extract" a value from a for the only way to manage is working on the last iteration, so you can do something like:

{% for item in items %}
{% if loop.last %} {# Do your stuff here #} {% endif %} {% endfor %}

 

If this answer helps you to solve your questions please mark it as a solution.

Thank you,


Gonzalo Torreras

HubSpot freelance developer

hola@gonzalotorreras.com
www.gonzalotorreras.com
Schedule a meeting
0 Avaliação positiva