Loading docs/howto/custom-template-tags.txt +33 −15 Original line number Diff line number Diff line Loading @@ -300,9 +300,12 @@ Template filter code falls into one of two situations: .. code-block:: python from django import template from django.utils.html import conditional_escape from django.utils.safestring import mark_safe register = template.Library() @register.filter(needs_autoescape=True) def initial_letter_filter(text, autoescape=None): first, other = text[0], text[1:] Loading Loading @@ -454,8 +457,9 @@ Continuing the above example, we need to define ``CurrentTimeNode``: .. code-block:: python from django import template import datetime from django import template class CurrentTimeNode(template.Node): def __init__(self, format_string): self.format_string = format_string Loading Loading @@ -498,6 +502,8 @@ The ``__init__`` method for the ``Context`` class takes a parameter called .. code-block:: python from django.template import Context def render(self, context): # ... new_context = Context({'var': obj}, autoescape=context.autoescape) Loading Loading @@ -545,7 +551,10 @@ A naive implementation of ``CycleNode`` might look something like this: .. code-block:: python class CycleNode(Node): import itertools from django import template class CycleNode(template.Node): def __init__(self, cyclevars): self.cycle_iter = itertools.cycle(cyclevars) def render(self, context): Loading Loading @@ -576,7 +585,7 @@ Let's refactor our ``CycleNode`` implementation to use the ``render_context``: .. code-block:: python class CycleNode(Node): class CycleNode(template.Node): def __init__(self, cyclevars): self.cyclevars = cyclevars def render(self, context): Loading Loading @@ -664,6 +673,7 @@ Now your tag should begin to look like this: .. code-block:: python from django import template def do_format_time(parser, token): try: # split_contents() knows not to split quoted strings. Loading Loading @@ -722,6 +732,11 @@ Our earlier ``current_time`` function could thus be written like this: .. code-block:: python import datetime from django import template register = template.Library() def current_time(format_string): return datetime.datetime.now().strftime(format_string) Loading Loading @@ -965,6 +980,9 @@ outputting it: .. code-block:: python import datetime from django import template class CurrentTimeNode2(template.Node): def __init__(self, format_string): self.format_string = format_string Loading docs/ref/templates/api.txt +7 −0 Original line number Diff line number Diff line Loading @@ -286,6 +286,7 @@ fully-populated dictionary to ``Context()``. But you can add and delete items from a ``Context`` object once it's been instantiated, too, using standard dictionary syntax:: >>> from django.template import Context >>> c = Context({"foo": "bar"}) >>> c['foo'] 'bar' Loading Loading @@ -397,6 +398,9 @@ Also, you can give ``RequestContext`` a list of additional processors, using the optional, third positional argument, ``processors``. In this example, the ``RequestContext`` instance gets a ``ip_address`` variable:: from django.http import HttpResponse from django.template import RequestContext def ip_address_processor(request): return {'ip_address': request.META['REMOTE_ADDR']} Loading @@ -417,6 +421,9 @@ optional, third positional argument, ``processors``. In this example, the :func:`~django.shortcuts.render_to_response()`: a ``RequestContext`` instance. Your code might look like this:: from django.shortcuts import render_to_response from django.template import RequestContext def some_view(request): # ... return render_to_response('my_template.html', Loading Loading
docs/howto/custom-template-tags.txt +33 −15 Original line number Diff line number Diff line Loading @@ -300,9 +300,12 @@ Template filter code falls into one of two situations: .. code-block:: python from django import template from django.utils.html import conditional_escape from django.utils.safestring import mark_safe register = template.Library() @register.filter(needs_autoescape=True) def initial_letter_filter(text, autoescape=None): first, other = text[0], text[1:] Loading Loading @@ -454,8 +457,9 @@ Continuing the above example, we need to define ``CurrentTimeNode``: .. code-block:: python from django import template import datetime from django import template class CurrentTimeNode(template.Node): def __init__(self, format_string): self.format_string = format_string Loading Loading @@ -498,6 +502,8 @@ The ``__init__`` method for the ``Context`` class takes a parameter called .. code-block:: python from django.template import Context def render(self, context): # ... new_context = Context({'var': obj}, autoescape=context.autoescape) Loading Loading @@ -545,7 +551,10 @@ A naive implementation of ``CycleNode`` might look something like this: .. code-block:: python class CycleNode(Node): import itertools from django import template class CycleNode(template.Node): def __init__(self, cyclevars): self.cycle_iter = itertools.cycle(cyclevars) def render(self, context): Loading Loading @@ -576,7 +585,7 @@ Let's refactor our ``CycleNode`` implementation to use the ``render_context``: .. code-block:: python class CycleNode(Node): class CycleNode(template.Node): def __init__(self, cyclevars): self.cyclevars = cyclevars def render(self, context): Loading Loading @@ -664,6 +673,7 @@ Now your tag should begin to look like this: .. code-block:: python from django import template def do_format_time(parser, token): try: # split_contents() knows not to split quoted strings. Loading Loading @@ -722,6 +732,11 @@ Our earlier ``current_time`` function could thus be written like this: .. code-block:: python import datetime from django import template register = template.Library() def current_time(format_string): return datetime.datetime.now().strftime(format_string) Loading Loading @@ -965,6 +980,9 @@ outputting it: .. code-block:: python import datetime from django import template class CurrentTimeNode2(template.Node): def __init__(self, format_string): self.format_string = format_string Loading
docs/ref/templates/api.txt +7 −0 Original line number Diff line number Diff line Loading @@ -286,6 +286,7 @@ fully-populated dictionary to ``Context()``. But you can add and delete items from a ``Context`` object once it's been instantiated, too, using standard dictionary syntax:: >>> from django.template import Context >>> c = Context({"foo": "bar"}) >>> c['foo'] 'bar' Loading Loading @@ -397,6 +398,9 @@ Also, you can give ``RequestContext`` a list of additional processors, using the optional, third positional argument, ``processors``. In this example, the ``RequestContext`` instance gets a ``ip_address`` variable:: from django.http import HttpResponse from django.template import RequestContext def ip_address_processor(request): return {'ip_address': request.META['REMOTE_ADDR']} Loading @@ -417,6 +421,9 @@ optional, third positional argument, ``processors``. In this example, the :func:`~django.shortcuts.render_to_response()`: a ``RequestContext`` instance. Your code might look like this:: from django.shortcuts import render_to_response from django.template import RequestContext def some_view(request): # ... return render_to_response('my_template.html', Loading