Ticket #13730: needs_context.diff
File needs_context.diff, 2.1 KB (added by , 14 years ago) |
---|
-
django/template/__init__.py
566 566 arg_vals.append(mark_safe(arg)) 567 567 else: 568 568 arg_vals.append(arg.resolve(context)) 569 if getattr(func, 'needs_autoescape', False): 569 if getattr(func, 'needs_context', False): 570 new_obj = func(obj, context=context, *arg_vals) 571 elif getattr(func, 'needs_autoescape', False): 570 572 new_obj = func(obj, autoescape=context.autoescape, *arg_vals) 571 573 else: 572 574 new_obj = func(obj, *arg_vals) … … 801 803 802 804 def render(self, context): 803 805 return self.s 804 806 805 807 def _render_value_in_context(value, context): 806 808 """ 807 809 Converts any value to a string to become part of a rendered template. This -
docs/howto/custom-template-tags.txt
305 305 handle the auto-escaping issues and return a safe string, the 306 306 ``is_safe`` attribute won't change anything either way. 307 307 308 Context filters 309 ~~~~~~~~~~~~~~~~~~~~~~~~~ 310 311 .. versionadded:: 1.2 312 313 Filters can behave diffrently depending on context. To make your filter get 314 context, set the ``needs_context`` attribute to ``True`` on your function 315 For example, this filter will transform given time to time zone specified 316 in context: 317 318 import pytz 319 from django.conf import settings 320 321 def localtime(dt, context=None): 322 if dt.tzinfo is None: 323 dt = pytz.timezone(settings.TIME_ZONE).localize(dt) 324 return dt.astimezone(context['timezone']) 325 localtime.needs_context = True 326 327 Of course, the 'timezone' key should be written into context to use it. 328 Also, you can't use both ``needs_context`` and ``needs_autoescape``. 329 308 330 Writing custom template tags 309 331 ---------------------------- 310 332