Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionals: allow string comparism #165

Open
ChrisKaun opened this issue Nov 27, 2020 · 4 comments
Open

Conditionals: allow string comparism #165

ChrisKaun opened this issue Nov 27, 2020 · 4 comments

Comments

@ChrisKaun
Copy link

We have a unique usecase for one of our customers who would like to display a string property dependent on it's value in different font-colors. This would require us to do something like this:

«car.color:if(val == 'green')»
  «=car.color» but marked in green
«car.color:elsif(val == 'red')»
  «=car.color» but marked in red
«car.color:endIf»

Of course it would be possible to create a getter method in rails that checks each color but since this is a really unique use case it wouldn't make any sense to hardcode this into our software logic.

@senny
Copy link
Owner

senny commented Nov 27, 2020

The idea behind the sablon template language is that templates should contain the least amount of knowledge possible. The reason is that they are hard to test and prone to errors when making changes. For that reason, such convenience operators are not provided. As you already deduced, the way to achieve this, would be by defining predicate methods on some object that you can check from the template.

@stadelmanma
Copy link
Collaborator

@ChrisKaun another good option might be to pre-process the context supplied by the user so you can streamline processing on the template side. I had several areas of complex logic and instead of trying to process that in the template I pre-processed stuff into several chunks of HTML and injected that into the document. One additional benefit of having most of the logic done outside of the template is that it lends itself to easier testing and validation.

@ChrisKaun
Copy link
Author

The idea behind the sablon template language is that templates should contain the least amount of knowledge possible. The reason is that they are hard to test and prone to errors when making changes. For that reason, such convenience operators are not provided. As you already deduced, the way to achieve this, would be by defining predicate methods on some object that you can check from the template.

The problem with his approach is that each time a new color must be added the methods must be updated.

another good option might be to pre-process the context supplied by the user so you can streamline processing on the template side. I had several areas of complex logic and instead of trying to process that in the template I pre-processed stuff into several chunks of HTML and injected that into the document. One additional benefit of having most of the logic done outside of the template is that it lends itself to easier testing and validation.

If I understand that correctly this still would require changes in the pre-process logic each time the customer of ours requires a new color, which I would love to skip.

@arvanasse
Copy link

@ChrisKaun Using sablon within a rails framework, I've achieved what you're suggesting by wrapping the string in ActiveSupport::StringInquirer.

«car.color:if(green?)»
  «=car.color» but marked in green
«car.color:elsif(red?)»
  «=car.color» but marked in red
«car.color:endIf»

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants