-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
optionally use a filter based on its existence #842
Comments
To me it sounds like undefined filters/tests should behave likes undefined function calls: i.e. fail when they are actually executed, but not when they are skipped by an if block around them. |
Regardless of whether it's considered a feature or bug I guess, in 2.10 and master I also get TemplateSyntaxErrors easily. If I put many things that shouldn't work inside Examples of things that fail right now:
An argument against changing this behavior is that the errors may be useful for debugging during normal template development, i.e., not when taking advantage of ignoring things inside an if block. Personally, unless there's a good reason I haven't thought of yet, I'm in favor of changing the behavior. I'd say skip the execution of the if blocks when the test fails. |
The first two failing make sense - invalid nesting is an obvious syntax error, and since But for a filter this doesn't seem to be needed, since |
Should that be the case though, if it's within a failing test block like I don't mean to belabor this point; it's fine if that shouldn't change. I may just misunderstand. |
What I mean is that the Python code for |
In 3.0 it will be possible to do the following:
|
I propose a feature, where within a template, you can test for the existence of a filter so that you can then use that filter if it exists, and not if it doesn't.
For example,
Currently, if the filter does not exist, Jinja would throw a
jinja2.exceptions.TemplateAssertionError: no filter named 'filter'
because of the line{{ var|filter }}
. The desired output is what is rendered in theelse
. You can test if the filter is callable, but you cannot then optionally use it. At a high level this is similar to how one might commonly use a var, if it exists.Example Use Case:
I would like to be able to write Lektor themes such that they can take advantage of filters provided by various Lektor plugins if they are installed. I do not want to require those plugins, and thus also their provided filters. I'd want a Lektor theme to flexibly handle either situation, as per the users' desire.
More generally, Jinja templates in any Flask or Django project could be written more flexibly to handle the optional presence filters. Adding custom or 3rd party filters is a common thing to do. Passing templates around between projects would be easier if they didn't force implementation of custom filters.
Implementation:
The desired effect could be achieved more than one way. For example, at least
not raising an exception when evaluating
{{ var|filter }}
and instead returning (basically anything) would allow the above snippet to be rendered.change the way the entire
if
is evaluated, such that the expressions contained in the contents of the "true" section are evaluated only if the test passes. Evaluation would resume upon reaching anelse
orendif
.My intuition was that the second option was already happening, but it is not. Though the contents of the passing section of the
if
never get to the final rendered product, the expressions are still evaluated anyway. Otherwise no exception would be rendered. If 2. happened, that could mean much nonsense could be included in between theif
and theelse
, and not trip exceptions. That sounds like a change that's worth some careful thought, but I can't immediately think of a problem with it. I am not intimately familiar with Jinja's internals. Exceptions would still be raised if the test ever actually passes.Skipping that section may also mean performance improvements, which is a nice side effect.
The text was updated successfully, but these errors were encountered: