Comments (14)
@AliSoftware I don't know about anything
from stencilswiftkit.
@djbe You can't hide 😄
from stencilswiftkit.
Sorry @claire-lynch-okcupid I don't know what was the outcome of this. Unfortunately I did not work with StencilKit lately.
from stencilswiftkit.
The thing is that for Stencil, everything that you output is a String
. The result of a map iteration will always be a String
. What you're doing here is actually mapping your array of variables to a list of variable descriptions and empty strings (for those that don't match your if
)
What you could do instead is filter in the for loop:
{% for variable in vars where variable.readAccess != "private" and variable.readAccess != "fileprivate" %}{{ variable.name }}: {{ variable.name }}{% if not forloop.last %}, {% endif %}{% endfor %}
from stencilswiftkit.
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.
from stencilswiftkit.
What's useful then is to separate that filter logic into a macro, which returns "true"
or "false"
, and set a variable to that result inside your for loop. Then check the value with an if test:
{% macro myMacro variable %}{% filter removeNewlines:"all" %}
{% if some long test %}true{%else %}false{% endif %}
{% endfilter %}{% endmacro %}
...
{% for variable in vars %}{% filter removeNewlines:"leading" %}
{% set shouldShow %}{% call myMacro variable %}{% endset %}
{% if shouldShow == "true" %}
{{ variable.name }}: {{ variable.name }}
{% if not forloop.last %}, {% endif %}{% endfor %}
{% endif %}
{% endfilter %}{% endfor %}
from stencilswiftkit.
Thanks for that pattern. I am gonna try this tomorrow.
from stencilswiftkit.
PS: just missing an {% endfilter %}
in the macro and for blocks of your example @djbe 😉
from stencilswiftkit.
This works. Thanks. But the {% if not forloop.last %}, {% endif %}
does not work anymore since we don't now if the last variable is filtered by the macro.
PS: just an {% endfor %}
after {% if not forloop.last %}, {% endif %}
too much in your example @djbe 😉
from stencilswiftkit.
One idea I pitched in our Slack was to create a new filter named call
in StencilSwiftKit, that would take as a parameter the name of a 1-arity macro defined in your template, and call it like the call tag does.
That would allow you to use the filter syntax to call macros that take one parameter, like this {{ myvariable|call:"myMacro" }}
… as an alternative way of using the set + call tags syntax {% set shouldShow %}{% call myMacro myvariable %}{% endset %}
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:
{% for variable in vars where variable|call:"myMacro" == "true" %}{% filter removeNewlines:"all" %}
{{ variable.name }}: {{ variable.name }}
{% if not forloop.last %}, {% endif %}
{% endfilter %}{% endfor %}
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 forloop.last
too!
If you feel like this addition would be interesting and worth it, we'd welcome a PR to add it 😉
from stencilswiftkit.
That would a nice addition as the filtering is would ne named and separated from the template. I am going to experiment with this
from stencilswiftkit.
I think this can not be implemented as a filter since
protocol FilterType {
func invoke(value: Any?, arguments: [Any?]) throws -> Any?
}
does not get the context to resolve the CallableBlock from the macro tag.
from stencilswiftkit.
@cornr stencilproject/Stencil#203 was merged a while ago that adds a context: Context
parameter to the invoke
function, so that shouldn't be an issue. It hasn't been released yet, but you can test the latest "master" branch of Stencil.
from stencilswiftkit.
@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, {* if myVariable == "true" *}
it doesn't ever evaluate to true because myVariable
isn't a string. Suggestions?
from stencilswiftkit.
Related Issues (20)
- Change our filters to accept a `mode` parameter instead of a boolean.
- SwiftIdentifier filter: add modes for more flexibility
- Replace filter: unescape backslashes HOT 7
- Make `Filter`s public HOT 2
- Test Swift 4.2 (or drop support for it) HOT 4
- swiftIdentifier converts all caps to title case HOT 3
- new release
- HEX Data for Colors.xcassets HOT 1
- question: is map supported for dictionaries? HOT 1
- Adding spm dependency on StencilSwiftKit causes warning in project, soon to be error HOT 2
- question: how to combine keys of multiple dictionaries in a single list and remove duplicates
- How can I keep {{ }} in stencil?
- Update to latest Stencil
- Add new increment filter to Numbers
- `set` tag not respecting scope in `for` loop HOT 8
- Is it possible to use it with Sourcery? HOT 1
- reusable macro templates HOT 4
- Build failure using Swift 4.2 from Xcode 10 Beta HOT 4
- Release 2.6.0 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stencilswiftkit.