-
Notifications
You must be signed in to change notification settings - Fork 277
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
"if" tag doesn't behave like Jinja when testing absent environment variable #660
Comments
|
For 1. I think it's actually a good thing: in the same spirit as Rust, requiring explicit typing makes it simpler and removes doubts such as "is 'false' false" and potential footguns. I don't use templating all that often so maybe there are other aspects that I missed though. |
@DuckDuckWhale That example is not relevant, because no programming language considers a string But from human perception, it is reasonable that empty string |
Right, that's not a very good example. Better examples would be To clarify on my original point: requiring explicit comparison instead of implicitly casting from other types to |
@DuckDuckWhale The example of "is [] false" and JS behavior on truthy/falsy is also not relevant here, because as I stated right from the beginning, Tera is inspired from Jinja, so we just follow the logic from Python & Jinja. I cannot imagine that someone is writing Jinja/Python code but using the behavior of JS. Moreover, if you take JS as standard, even |
It is very much relevant here as Python also has the concept of truthy/falsy. I just picked JS as an example instead. Mimicking Jinja2 is a good point though. However, Tera is a Rust template engine and doesn't aim to be 100% compatible. For folks coming form Rust this may be a friction point. Although I prefer being explicit, I don't know the design principles of this project so this is ultimately @Keats's call. |
I don't object that "Python also has the concept of truthy/falsy". But between JS-style of truthy/falsy (where some empty data is not equivalent to It will be brain hack if you are writing Jinja code but have to "think" JS way. It is quite natural to write Jinja code and think the Python way. I also agree that Tera won't be 100% compatible, so I still agree that |
Tera is definitely using the truthiness checks from Python/Jinja2, not from any other languages. |
As stated above my point is that from a language design point of view the existence of truthiness/falsiness itself is ambiguous and may be error-prone, and I used JS only as an example language that has this concept. This is the decision taken by the Rust community and it's far from "ridiculous". However I understand if that's a not a goal and Tera tries to be conservative in divergence from Jinja2. |
For example, I want to check if an environment variable is defined, then do something. Coming from Python + Jinja, I expect to have this code:
But it doesn't work. I end up having to write:
There are many limitations:
if
doesn't consider empty string equivalent tofalse
(error: "Functionget_env
was used in a logic operation but is not returning a bool"). I have to compare with another empty string.name
parameter must be explicitly written.default
(error: "Environment variableTRACKING
not found"). In Python,os.getenv(key)
is equivalent toos.getenv(key, default=None)
, so you don't have to always writedefault
.The text was updated successfully, but these errors were encountered: