On Sat, 2006-06-10 at 14:57 +0000, Gary Wilson wrote:
> Simon Willison wrote:
> > On 10 Jun 2006, at 07:39, Malcolm Tredinnick wrote:
> >
> > > What's our policy on template filters failing? If applying a filter
> > > raises an error, should we just trap it and return the empty
> > > string, or
> > > raise an error? I can't find this written down anywhere, but maybe
> > > I've
> > > missed it.
> >
> > The original policy has always been that errors in template filters
> > should fail silently - the idea being that it shouldn't be possible
> > to "break" a site by editing a template.
>
> Just to be clear, this following is quoted from
> http://www.djangoproject.com/documentation/templates_python/#writing-custom-template-filters
>
> "Filter functions should always return something. They shouldn't raise
> exceptions. They should fail silently. In case of error, they should
> return either the original input or an empty string -- whichever makes
> more sense."
>
> > With hindsight, I think that this was a mistake. An error in a
> > template is still an error, and needs to be caught. Failing silently
> > just means pages stay broken without the website maintenance team
> > hearing about it.
Ah, I checked the wrong document: I looked in the "template users"
document, not the "template developers" one.
> What are the altervatives though? If you raised an error for a
> template variable that wasn't defined, you wouldn't be able to do
> {{ possibly_undefined }}
> or
> {% if possibly_undefined %}{{ possibly_undefined }}{% endif %}
>
> Letting undefined variables return empty strings is both flexible and
> convenient.
This was actually the motivation for asking the original question. Your
above example uses only template *tags*, not *filters*. I was thinking
that if somebody had made the effort to include the data, then it's not
unreasonable to expect them to include it in the right form. And if the
data is going to possibly undefined, then something like the above test
should be used first.
But since we have a documented policy and the problem here is just my
inability to read, this issue is moot.
Thanks,
Malcolm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-developers
-~----------~----~----~----~------~----~------~--~---