Replies: 14 comments
-
The thing is that for Stencil, everything that you output is a What you could do instead is filter in the for loop:
|
Beta Was this translation helpful? Give feedback.
-
Ok. The filter in the for loop is what I am doing right now. And sure it does work. But I use this for loop and therefore the filter several times and the condition is a lot more complex. Thats why I want to filter the array once and then iterate over it at different places. |
Beta Was this translation helpful? Give feedback.
-
What's useful then is to separate that filter logic into a macro, which returns
|
Beta Was this translation helpful? Give feedback.
-
Thanks for that pattern. I am gonna try this tomorrow. |
Beta Was this translation helpful? Give feedback.
-
PS: just missing an |
Beta Was this translation helpful? Give feedback.
-
@AliSoftware I don't know about anything |
Beta Was this translation helpful? Give feedback.
-
@djbe You can't hide 😄 |
Beta Was this translation helpful? Give feedback.
-
This works. Thanks. But the PS: just an |
Beta Was this translation helpful? Give feedback.
-
One idea I pitched in our Slack was to create a new filter named That would allow you to use the filter syntax to call macros that take one parameter, like this note that this filter would only accept macros that have exactly one parameter then, and should throw a TemplateError otherwise If we decide to add such a convenience filter in StencilSwiftKit, that means the code suggested by @djbe would become something like this:
And given that you'd be able to use the "for where" syntax in the for tag, I think that would fix the issue you mention with the If you feel like this addition would be interesting and worth it, we'd welcome a PR to add it 😉 |
Beta Was this translation helpful? Give feedback.
-
That would a nice addition as the filtering is would ne named and separated from the template. I am going to experiment with this |
Beta Was this translation helpful? Give feedback.
-
I think this can not be implemented as a filter since
does not get the context to resolve the CallableBlock from the macro tag. |
Beta Was this translation helpful? Give feedback.
-
@cornr stencilproject/Stencil#203 was merged a while ago that adds a |
Beta Was this translation helpful? Give feedback.
-
@djbe @cornr it's been a long time but do you remember whether you got this working? I read in the Stencil docs, "The equality operator only supports numerical, string and boolean types." So when I attempt to evaluate something like, |
Beta Was this translation helpful? Give feedback.
-
Sorry @claire-lynch-okcupid I don't know what was the outcome of this. Unfortunately I did not work with StencilKit lately. |
Beta Was this translation helpful? Give feedback.
-
I am trying to filter an array using
map
. In this case I filter Sourcery variables:{% map type.allVariables into vars using variable %}{% if variable.readAccess != "private" and variable.readAccess != "fileprivate" and not variable.isComputed %}{{ variable }}{% endif %}{% endmap %}
Afterwards I want to print these filtered vars.
{% for variable in vars where variable != "" %}{{ variable.name }}: {{ variable.name }}{% if not forloop.last %}, {% endif %}{% endfor %}
A get following Exception:
My guess is the problem lies here:
StencilSwiftKit/Sources/StencilSwiftKit/MapNode.swift
Line 71 in d5f90b5
The MapNode expects the mapped 0utput to be a String.
I could dig into this and provide a fix if you recognize it as an issue.
Beta Was this translation helpful? Give feedback.
All reactions