Closed hebertialmeida closed 7 years ago
Hi @hebertialmeida,
Did you check that your list
is not empty in the context?
You could add {{ list }}
to your stencil to print it to check that you are passing something to map
.
If you have elements – meaning map
does not work – could you please provide us the context you are using with your stencil so we can reproduce the bug?
@hebertialmeida Are you also printing the result variables? A map tag, by definition, does not print it's results, but stores them in a variable.
Try this instead:
// map the list without item name
{% map list into result1 %}{{maploop.item|uppercase}}{% endmap %}
// print items in result1
{% for item in result1 %}
- {{item}}
{% endfor %}
Hi @Liquidsoul Yes, I have checked.
@djbe It works with for
function and result1
, but the {{maploop.item|uppercase}}
don't print the item.
Also in this case:
{% map list into result3 using item %}{{maploop.counter}} - {{item}}{% endmap %}
The {{maploop.counter}} - {{item}}
don't print the count and and item.
Anything that I put inside the map
tags are not working for me.
@hebertialmeida that's my point. As mentioned in the documentation, the map
tag never prints anything, it transforms a variable into another variable.
After using map, you can do something else with it, such as printing it, that's up to you.
Whatever you "print" inside a map loop, gets stored in a new item and added to the result array. If you want to directly print something without transforming, just use the for
tag.
I see, but on docs you have this example {% map list into result2 using item %}{{item}}{{item|uppercase}}{% endmap %}
Don't the using item
at the end have the item inside the map?
Also maploop.item
, maploop.counter
, maploop.first
, maploop.last
are not to be used inside the map tag?
This
{% map list into result2 using item %}{{item}}{{item|uppercase}}{% endmap %}
Is the equivalent of doing this:
let result2 = list.map { item in
return "\(item)\(item.uppercase)"
}
If you provide using item
, we'll provide that variable inside the map loop. The other variables you mentioned will always be available inside the maploop, and you can use them whenever you want.
I understand, my bad :) I was trying to print item inside the map, but should do that on for and the modifications on map. Thank you...
I'll see about improving the documentation to make the sample code a bit clearer. Essentially map
is a combination of the for
and set
tags.
Yeah, specially when you have swift equivalent makes me visualize and understand what I was doing wrong.
Thanks for the tip!
I have been trying to use the map function but without success. I used this documentation sample https://github.com/SwiftGen/StencilSwiftKit/blob/master/Documentation/tag-map.md
This is what it is rendering, always empty.