Re: Template.render() should accept a dictionary, not just a Context object
On Fri, Apr 11, 2008 at 12:02 PM, Patryk Zawadzki <[EMAIL PROTECTED]> wrote: > Why not use the opposite test and check for instances of dict? Because then you have to pass something that subclasses dict instead of something that implements the interface of dict. We could go on and on about this all day. The usual way to approach something like this would be to check for some attribute of contexts that aren't part of the standard dictionary protocol. Since contexts work mostly like dicts, the only real differences are the push() and pop() methods, which dicts don't support. Welcome to duck typing. def render(self, context): "Display stage -- can be called many times" if not hasattr(context, 'push'): context = Context(context) return self.nodelist.render(context) Of course, I should note that I actually don't have an opinion on this subject. I'm merely offering an option to suit both sides of the equation. -Gul --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~--~~~~--~~--~--~---
Re: Template.render() should accept a dictionary, not just a Context object
On Fri, Apr 11, 2008 at 5:49 PM, Rajeev J Sebastian <[EMAIL PROTECTED]> wrote: > On Fri, Apr 11, 2008 at 8:56 PM, Simon Willison <[EMAIL PROTECTED]> wrote: > > > > def render(self, context): > > "Display stage -- can be called many times" > > if not isinstance(context, Context): > > context = Context(context) > > return self.nodelist.render(context) > > > > This is backwards compatible and would make the template system that > > tiny bit nicer to use. > The problem with this is that we have to pass an instance of a > subclass of Context, rather than something that has the interface of a > Context. Is it a good idea ? Why not use the opposite test and check for instances of dict? -- Patryk Zawadzki PLD Linux Distribution --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~--~~~~--~~--~--~---
Re: Template.render() should accept a dictionary, not just a Context object
On Fri, Apr 11, 2008 at 8:56 PM, Simon Willison <[EMAIL PROTECTED]> wrote: > > def render(self, context): > "Display stage -- can be called many times" > if not isinstance(context, Context): > context = Context(context) > return self.nodelist.render(context) > > This is backwards compatible and would make the template system that > tiny bit nicer to use. The problem with this is that we have to pass an instance of a subclass of Context, rather than something that has the interface of a Context. Is it a good idea ? Regards Rajeev J Sebastian --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~--~~~~--~~--~--~---
Template.render() should accept a dictionary, not just a Context object
Here's something that has been quietly bugging me about Django for / years/: Context() is an implementation detail of the template engine. Having to instantiate a Context just to pass it to render() is unnecessary boilerplate (and means you have to import another symbol as well). Template.render() should continue to accept a context object, but it should also have the option of accepting a dictionary - and if it gets one, should quietly upgrade it to a Context before passing it down the render chain. http://code.djangoproject.com/browser/django/trunk/django/template/__init__.py#L174 Suggested implementation: def render(self, context): "Display stage -- can be called many times" if not isinstance(context, Context): context = Context(context) return self.nodelist.render(context) This is backwards compatible and would make the template system that tiny bit nicer to use. Cheers, Simon --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~--~~~~--~~--~--~---