mirror of
https://github.com/django/django.git
synced 2025-11-25 21:22:14 +00:00
- The include tag now has a 'with' option to include to provide extra context
vairables to the included template.
- The include tag now has an 'only' option to exclude the current context
when rendering the included template.
- The with tag now accepts multiple variable assignments.
- The with, include and blocktrans tags now use a new keyword argument format
for variable assignments (e.g. `{% with foo=1 bar=2 %}`).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14922 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
99742d8d73
commit
3ae9117c46
8 changed files with 274 additions and 83 deletions
|
|
@ -206,7 +206,7 @@ many-to-many relation to User, the following template code is optimal:
|
|||
.. code-block:: html+django
|
||||
|
||||
{% if display_inbox %}
|
||||
{% with user.emails.all as emails %}
|
||||
{% with emails=user.emails.all %}
|
||||
{% if emails %}
|
||||
<p>You have {{ emails|length }} email(s)</p>
|
||||
{% for email in emails %}
|
||||
|
|
|
|||
|
|
@ -371,12 +371,11 @@ using the :ttag:`include` tag to reuse it in other templates::
|
|||
{% endfor %}
|
||||
|
||||
If the form object passed to a template has a different name within the
|
||||
context, you can alias it using the :ttag:`with` tag::
|
||||
context, you can alias it using the ``with`` argument of the :ttag:`include`
|
||||
tag::
|
||||
|
||||
<form action="/comments/add/" method="post">
|
||||
{% with comment_form as form %}
|
||||
{% include "form_snippet.html" %}
|
||||
{% endwith %}
|
||||
{% include "form_snippet.html" with form=comment_form %}
|
||||
<p><input type="submit" value="Submit comment" /></p>
|
||||
</form>
|
||||
|
||||
|
|
|
|||
|
|
@ -438,6 +438,9 @@ It's not possible to mix a template variable inside a string within ``{% trans
|
|||
``blocktrans`` template tag
|
||||
---------------------------
|
||||
|
||||
.. versionchanged:: 1.3
|
||||
New keyword argument format.
|
||||
|
||||
Contrarily to the ``trans`` tag, the ``blocktrans`` tag allows you to mark
|
||||
complex sentences consisting of literals and variable content for translation
|
||||
by making use of placeholders::
|
||||
|
|
@ -448,18 +451,18 @@ To translate a template expression -- say, accessing object attributes or
|
|||
using template filters -- you need to bind the expression to a local variable
|
||||
for use within the translation block. Examples::
|
||||
|
||||
{% blocktrans with article.price as amount %}
|
||||
{% blocktrans with amount=article.price %}
|
||||
That will cost $ {{ amount }}.
|
||||
{% endblocktrans %}
|
||||
|
||||
{% blocktrans with value|filter as myvar %}
|
||||
{% blocktrans with myvar=value|filter %}
|
||||
This will have {{ myvar }} inside.
|
||||
{% endblocktrans %}
|
||||
|
||||
If you need to bind more than one expression inside a ``blocktrans`` tag,
|
||||
separate the pieces with ``and``::
|
||||
|
||||
{% blocktrans with book|title as book_t and author|title as author_t %}
|
||||
{% blocktrans with book_t=book|title author_t=author|title %}
|
||||
This is {{ book_t }} by {{ author_t }}
|
||||
{% endblocktrans %}
|
||||
|
||||
|
|
@ -474,7 +477,7 @@ This tag also provides for pluralization. To use it:
|
|||
|
||||
An example::
|
||||
|
||||
{% blocktrans count list|length as counter %}
|
||||
{% blocktrans count counter=list|length %}
|
||||
There is only one {{ name }} object.
|
||||
{% plural %}
|
||||
There are {{ counter }} {{ name }} objects.
|
||||
|
|
@ -482,7 +485,7 @@ An example::
|
|||
|
||||
A more complex example::
|
||||
|
||||
{% blocktrans with article.price as amount count i.length as years %}
|
||||
{% blocktrans with amount=article.price count years=i.length %}
|
||||
That will cost $ {{ amount }} per year.
|
||||
{% plural %}
|
||||
That will cost $ {{ amount }} per {{ years }} years.
|
||||
|
|
@ -494,6 +497,9 @@ construct is internally converted to an ``ungettext`` call. This means the
|
|||
same :ref:`notes regarding ungettext variables <pluralization-var-notes>`
|
||||
apply.
|
||||
|
||||
.. note:: The previous more verbose format is still supported:
|
||||
``{% blocktrans with book|title as book_t and author|title as author_t %}``
|
||||
|
||||
.. _template-translation-vars:
|
||||
|
||||
Other tags
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue