krzysztofzablocki / Sourcery

Meta-programming for Swift, stop writing boilerplate code.
http://merowing.info
MIT License
7.66k stars 616 forks source link

Stencil if-tag doesn't support filters #102

Closed marinbenc closed 7 years ago

marinbenc commented 7 years ago

With the template

{% if "FooBar"|contains: "Foo" %}
FooBar contains D
{% endif %}

I get the error: 'if' expression error: dangling token

However, when I replace the custom contains filter with a built-in operator the file generates normally.

{% if "FooBar" == "FooBar" %}
FooBar contains D
{% endif %}

Am I doing something wrong?

mbarclay commented 7 years ago

I am not so sure Stencil has a contains filter http://stencil.fuller.li/en/latest/builtins.html#built-in-filters But would love to be wrong :] Oh re-reading you have a custom filter? I'd like to learn more.

krzysztofzablocki commented 7 years ago

we have a custom filter, but looking at Stencil source I'm not sure if if tags support filters in current version? did you use them before @alisoftware @ilyapuchka ?

ilyapuchka commented 7 years ago

Yep, I'm also not sure if Stencil actually supports filters in if. Using {{ FooBar|country: "Foo" }}works fine though. Probably we need to file an issue in Stencil repo to improve that. /cc @kylef

AliSoftware commented 7 years ago

I don't think so either.

One workaround would be to use the {% set %} node I use in SwiftGen (and soon available via SwiftGen/StencilSwiftKit onces we finish the Great Split of SwiftGen) to set it result of the filter into a temporary variable then do the if on it. Might be a workaround if SwiftGen/StencilSwiftKit comes before any PR on Stencil to support filters in if nodes.

(Of course in long term supporting filters in if expressions would indeed be better).

AliSoftware commented 7 years ago

BTW @krzysztofzablocki even if SwiftGen's Great Split isn't finished yet, I think you may already start using https://github.com/SwiftGen/StencilSwiftKit as a pod dependency for Sourcery.

We still have to create an Xcode projet, add the unit tests from SwiftGen for those nodes, configure Travis, Write the documentation, etc… but the podspec already works, so even if it's not yet pushed to trunk you should totally be able to use pod 'StencilSwiftKit', :git => 'https://github.com/SwiftGen/StencilSwiftKit' already to give it a try :wink:

marinbenc commented 7 years ago

I didn't realise Stencil did not support custom filters in an if statement. In the Sourcery documentation, there's this line

{% if name|contains: "Foo" %} - check if name contains arbitrary substring

I guess that confused me.

krzysztofzablocki commented 7 years ago

yeah I think we made assumption it will work (it really should), we mostly use filters on for loops and in those it works :)

krzysztofzablocki commented 7 years ago

https://github.com/kylef/Stencil/issues/90