Fixed #9091 -- Rephrased Variable() documentation. Thanks, telenieko

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9044 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2008-09-16 05:42:11 +00:00
parent 883aa6b9c8
commit 1fcf33095f
2 changed files with 12 additions and 40 deletions

View File

@ -523,58 +523,30 @@ Now your tag should begin to look like this::
raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name
return FormatTimeNode(date_to_be_formatted, format_string[1:-1]) return FormatTimeNode(date_to_be_formatted, format_string[1:-1])
.. versionchanged:: 1.0
Variable resolution has changed in the 1.0 release of Django. ``template.resolve_variable()``
has been deprecated in favor of a new ``template.Variable`` class.
You also have to change the renderer to retrieve the actual contents of the You also have to change the renderer to retrieve the actual contents of the
``date_updated`` property of the ``blog_entry`` object. This can be ``date_updated`` property of the ``blog_entry`` object. This can be
accomplished by using the ``resolve_variable()`` function in accomplished by using the ``Variable()`` class in ``django.template``.
``django.template``. You pass ``resolve_variable()`` the variable name and the
current context, available in the ``render`` method:: To use the ``Variable`` class, simply instantiate it with the name of the
variable to be resolved, and then call ``variable.resolve(context)``. So,
for example::
from django import template
from django.template import resolve_variable
import datetime
class FormatTimeNode(template.Node): class FormatTimeNode(template.Node):
def __init__(self, date_to_be_formatted, format_string): def __init__(self, date_to_be_formatted, format_string):
self.date_to_be_formatted = date_to_be_formatted self.date_to_be_formatted = Variable(date_to_be_formatted)
self.format_string = format_string self.format_string = format_string
def render(self, context): def render(self, context):
try: try:
actual_date = resolve_variable(self.date_to_be_formatted, context) actual_date = self.date_to_be_formatted.resolve(context)
return actual_date.strftime(self.format_string) return actual_date.strftime(self.format_string)
except template.VariableDoesNotExist: except template.VariableDoesNotExist:
return '' return ''
``resolve_variable`` will try to resolve ``blog_entry.date_updated`` and then
format it accordingly.
.. versionadded:: 1.0
Variable resolution has changed in the development version of Django.
``template.resolve_variable()`` is still available, but has been deprecated
in favor of a new ``template.Variable`` class. Using this class will usually
be more efficient than calling ``template.resolve_variable``
To use the ``Variable`` class, simply instantiate it with the name of the
variable to be resolved, and then call ``variable.resolve(context)``. So,
in the development version, the above example would be more correctly
written as:
.. parsed-literal::
class FormatTimeNode(template.Node):
def __init__(self, date_to_be_formatted, format_string):
self.date_to_be_formatted = **Variable(date_to_be_formatted)**
self.format_string = format_string
def render(self, context):
try:
actual_date = **self.date_to_be_formatted.resolve(context)**
return actual_date.strftime(self.format_string)
except template.VariableDoesNotExist:
return ''
Changes are highlighted in bold.
Variable resolution will throw a ``VariableDoesNotExist`` exception if it cannot Variable resolution will throw a ``VariableDoesNotExist`` exception if it cannot
resolve the string passed to it in the current context of the page. resolve the string passed to it in the current context of the page.

View File

@ -348,7 +348,7 @@ In addition, ``QueryDict`` has the following methods:
.. method:: QueryDict.lists() .. method:: QueryDict.lists()
Like :method:items(), except it includes all values, as a list, for each Like :meth:`items()`, except it includes all values, as a list, for each
member of the dictionary. For example:: member of the dictionary. For example::
>>> q = QueryDict('a=1&a=2&a=3') >>> q = QueryDict('a=1&a=2&a=3')