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
A "AttributeNotFoundExceptionHandler" interface would be nice for custom Filters. #223
Comments
Can you provide more information about your custom filter? I suspect that this is too niche of a problem that any solution is probably not worth the overhead. |
In the framework I work on, I use Pebble to output informations if fields from a submitted Form contain errors. For example, If "myForm.user.company.title" is invalid, I want to ouput an error message. But it is possible that "myForm.user.company" doesn't exist yet... In that case, I simply don't want to output anything. Note that I still want to use Using my workaround, I do something like : Since the If I try to do something similar but using a function, for example :
then an exception is thrown when "myForm.user.company" doesn't exist. In other words, I need the I can submit a Pull Request for that, it's trivial to implement. |
So, essentially you want to continue using I'm reluctant to allow this exception to be suppressed by arbitrary filters because it defeats the purpose of using My preference is to maintain status quo for now, but I'll keep this task open so that it can be revisited for the next major version of Pebble when we don't mind breaking backwards compatibility. At that time, I would want the solution to be crystal clear when reading the template that Some possible workarounds that you can implement with the current system:
|
Thanks for that reply. I understand your concerns, but I don't see how it would break any backwards compatibility or be confusing. You simply do:
Instead of and you make But at least there are a couple of workarounds, so it ok for me! Thanks for your help. |
The "confusing" problem with your proposed solution is when you read |
I see... At least, don't make |
There is another option to consider: Why not add a syntax that allows users to safely query for null values, even if ExampleLet's assume the The expression If I am absolutely aware of the fact that the user may be optional, I could make that explicit, i.e. via The filter mentioned above would simply reduce to the following then:
|
I still think that libraries and frameworks should ideally have the possibility to create functions and filters that are able to manage non existing attributes, without a special syntax... And even when I currently have my hands deep into form validation, for the framework I work on, and I must say that without that "extending the Let me give you more details about that already discussed "validation" use case... Displaying an HTML form would look like this in my framework (simplified example) :
I don't want to say to the developers : "You have to make sure that there are validation messages associated to a field before trying to output them.", that would be very ugly in my opinion... The Frameworks such as Spring MVC (with JSP for example) provide complex In other words, I still think that preventing third-parties to provide functions and filters that handle non existing attributes is somewhat limiting. That said, I see another option though : If the "expression parsing" algorithm Pebble uses was available using an API, then everything could work too. For example, if there was this method available :
Then we could write a function (or a filter) which would looks like : And the function/filter could evaluate this expression by itself, using the Sorry for the wall of text! :-) |
I now have a live example of what I'm talking about. Have a look at the first filter, I would have prefered to use functions there, but since there is no way to make them "attribute does not exist" tolerant, I used filters with the "extends I want the users to be able to have Anyway, it's just to show why a |
Related to #245 |
Currently, if I understand correctly, only the
DefaultFilter
filter will be called if an expression contains a not found attribute.It would be nice if there was an interface our custom filters could implement so they are applied even if an attribute is not found in the expression! My current workaround is to make my custom filter extend
DefaultFilter
, but an interface would be way cleaner.The text was updated successfully, but these errors were encountered: