App Engine Python SDK  v1.6.9 rev.445
The Python runtime is available as an experimental Preview feature.
Classes | Functions | Variables
google.appengine._internal.django.template.defaulttags Namespace Reference

Classes

class  AutoEscapeControlNode
 
class  CommentNode
 
class  CsrfTokenNode
 
class  CycleNode
 
class  DebugNode
 
class  FilterNode
 
class  FirstOfNode
 
class  ForNode
 
class  IfChangedNode
 
class  IfEqualNode
 
class  IfNode
 
class  LoadNode
 
class  NowNode
 
class  RegroupNode
 
class  SpacelessNode
 
class  SsiNode
 
class  TemplateIfParser
 
class  TemplateLiteral
 
class  TemplateTagNode
 
class  URLNode
 
class  WidthRatioNode
 
class  WithNode
 

Functions

def include_is_allowed
 
def autoescape
 
def comment
 
def cycle
 
def csrf_token
 
def debug
 
def do_filter
 
def firstof
 
def do_for
 
def do_ifequal
 
def ifequal
 
def ifnotequal
 
def do_if
 
def ifchanged
 
def ssi
 
def load
 
def now
 
def regroup
 
def spaceless
 
def templatetag
 
def url
 
def widthratio
 
def do_with
 

Variables

tuple register = Library()
 
tuple kwarg_re = re.compile(r"(?:(\w+)=)?(.+)")
 
tuple autoescape = register.tag(autoescape)
 
tuple comment = register.tag(comment)
 
tuple cycle = register.tag(cycle)
 
tuple debug = register.tag(debug)
 
tuple do_filter = register.tag("filter", do_filter)
 
tuple firstof = register.tag(firstof)
 
tuple do_for = register.tag("for", do_for)
 
tuple ifequal = register.tag(ifequal)
 
tuple ifnotequal = register.tag(ifnotequal)
 
tuple do_if = register.tag("if", do_if)
 
tuple ifchanged = register.tag(ifchanged)
 
tuple ssi = register.tag(ssi)
 
tuple load = register.tag(load)
 
tuple now = register.tag(now)
 
tuple regroup = register.tag(regroup)
 
tuple spaceless = register.tag(spaceless)
 
tuple templatetag = register.tag(templatetag)
 
tuple url = register.tag(url)
 
tuple widthratio = register.tag(widthratio)
 
tuple do_with = register.tag('with', do_with)
 

Detailed Description

Default tags used by the template system, available to all templates.

Function Documentation

def google.appengine._internal.django.template.defaulttags.autoescape (   parser,
  token 
)
Force autoescape behaviour for this block.
def google.appengine._internal.django.template.defaulttags.comment (   parser,
  token 
)
Ignores everything between ``{% comment %}`` and ``{% endcomment %}``.
def google.appengine._internal.django.template.defaulttags.cycle (   parser,
  token 
)
Cycles among the given strings each time this tag is encountered.

Within a loop, cycles among the given strings each time through
the loop::

    {% for o in some_list %}
        <tr class="{% cycle 'row1' 'row2' %}">
            ...
        </tr>
    {% endfor %}

Outside of a loop, give the values a unique name the first time you call
it, then use that name each sucessive time through::

        <tr class="{% cycle 'row1' 'row2' 'row3' as rowcolors %}">...</tr>
        <tr class="{% cycle rowcolors %}">...</tr>
        <tr class="{% cycle rowcolors %}">...</tr>

You can use any number of values, separated by spaces. Commas can also
be used to separate values; if a comma is used, the cycle values are
interpreted as literal strings.
def google.appengine._internal.django.template.defaulttags.debug (   parser,
  token 
)
Outputs a whole load of debugging information, including the current
context and imported modules.

Sample usage::

    <pre>
        {% debug %}
    </pre>
def google.appengine._internal.django.template.defaulttags.do_filter (   parser,
  token 
)
Filters the contents of the block through variable filters.

Filters can also be piped through each other, and they can have
arguments -- just like in variable syntax.

Sample usage::

    {% filter force_escape|lower %}
        This text will be HTML-escaped, and will appear in lowercase.
    {% endfilter %}
def google.appengine._internal.django.template.defaulttags.do_for (   parser,
  token 
)
Loops over each item in an array.

For example, to display a list of athletes given ``athlete_list``::

    <ul>
    {% for athlete in athlete_list %}
        <li>{{ athlete.name }}</li>
    {% endfor %}
    </ul>

You can loop over a list in reverse by using
``{% for obj in list reversed %}``.

You can also unpack multiple values from a two-dimensional array::

    {% for key,value in dict.items %}
        {{ key }}: {{ value }}
    {% endfor %}

The ``for`` tag can take an optional ``{% empty %}`` clause that will
be displayed if the given array is empty or could not be found::

    <ul>
      {% for athlete in athlete_list %}
        <li>{{ athlete.name }}</li>
      {% empty %}
        <li>Sorry, no athletes in this list.</li>
      {% endfor %}
    <ul>

The above is equivalent to -- but shorter, cleaner, and possibly faster
than -- the following::

    <ul>
      {% if althete_list %}
        {% for athlete in athlete_list %}
          <li>{{ athlete.name }}</li>
        {% endfor %}
      {% else %}
        <li>Sorry, no athletes in this list.</li>
      {% endif %}
    </ul>

The for loop sets a number of variables available within the loop:

    ==========================  ================================================
    Variable                    Description
    ==========================  ================================================
    ``forloop.counter``         The current iteration of the loop (1-indexed)
    ``forloop.counter0``        The current iteration of the loop (0-indexed)
    ``forloop.revcounter``      The number of iterations from the end of the
                                loop (1-indexed)
    ``forloop.revcounter0``     The number of iterations from the end of the
                                loop (0-indexed)
    ``forloop.first``           True if this is the first time through the loop
    ``forloop.last``            True if this is the last time through the loop
    ``forloop.parentloop``      For nested loops, this is the loop "above" the
                                current one
    ==========================  ================================================
def google.appengine._internal.django.template.defaulttags.do_if (   parser,
  token 
)
The ``{% if %}`` tag evaluates a variable, and if that variable is "true"
(i.e., exists, is not empty, and is not a false boolean value), the
contents of the block are output:

::

    {% if athlete_list %}
        Number of athletes: {{ athlete_list|count }}
    {% else %}
        No athletes.
    {% endif %}

In the above, if ``athlete_list`` is not empty, the number of athletes will
be displayed by the ``{{ athlete_list|count }}`` variable.

As you can see, the ``if`` tag can take an option ``{% else %}`` clause
that will be displayed if the test fails.

``if`` tags may use ``or``, ``and`` or ``not`` to test a number of
variables or to negate a given variable::

    {% if not athlete_list %}
        There are no athletes.
    {% endif %}

    {% if athlete_list or coach_list %}
        There are some athletes or some coaches.
    {% endif %}

    {% if athlete_list and coach_list %}
        Both atheletes and coaches are available.
    {% endif %}

    {% if not athlete_list or coach_list %}
        There are no athletes, or there are some coaches.
    {% endif %}

    {% if athlete_list and not coach_list %}
        There are some athletes and absolutely no coaches.
    {% endif %}

Comparison operators are also available, and the use of filters is also
allowed, for example:

    {% if articles|length >= 5 %}...{% endif %}

Arguments and operators _must_ have a space between them, so
``{% if 1>2 %}`` is not a valid if tag.

All supported operators are: ``or``, ``and``, ``in``, ``not in``
``==`` (or ``=``), ``!=``, ``>``, ``>=``, ``<`` and ``<=``.

Operator precedence follows Python.
def google.appengine._internal.django.template.defaulttags.do_with (   parser,
  token 
)
Adds a value to the context (inside of this block) for caching and easy
access.

For example::

    {% with person.some_sql_method as total %}
        {{ total }} object{{ total|pluralize }}
    {% endwith %}
def google.appengine._internal.django.template.defaulttags.firstof (   parser,
  token 
)
Outputs the first variable passed that is not False, without escaping.

Outputs nothing if all the passed variables are False.

Sample usage::

    {% firstof var1 var2 var3 %}

This is equivalent to::

    {% if var1 %}
        {{ var1|safe }}
    {% else %}{% if var2 %}
        {{ var2|safe }}
    {% else %}{% if var3 %}
        {{ var3|safe }}
    {% endif %}{% endif %}{% endif %}

but obviously much cleaner!

You can also use a literal string as a fallback value in case all
passed variables are False::

    {% firstof var1 var2 var3 "fallback value" %}

If you want to escape the output, use a filter tag::

    {% filter force_escape %}
        {% firstof var1 var2 var3 "fallback value" %}
    {% endfilter %}
def google.appengine._internal.django.template.defaulttags.ifchanged (   parser,
  token 
)
Checks if a value has changed from the last iteration of a loop.

The 'ifchanged' block tag is used within a loop. It has two possible uses.

1. Checks its own rendered contents against its previous state and only
   displays the content if it has changed. For example, this displays a
   list of days, only displaying the month if it changes::

        <h1>Archive for {{ year }}</h1>

        {% for date in days %}
            {% ifchanged %}<h3>{{ date|date:"F" }}</h3>{% endifchanged %}
            <a href="{{ date|date:"M/d"|lower }}/">{{ date|date:"j" }}</a>
        {% endfor %}

2. If given a variable, check whether that variable has changed.
   For example, the following shows the date every time it changes, but
   only shows the hour if both the hour and the date have changed::

        {% for date in days %}
            {% ifchanged date.date %} {{ date.date }} {% endifchanged %}
            {% ifchanged date.hour date.date %}
                {{ date.hour }}
            {% endifchanged %}
        {% endfor %}
def google.appengine._internal.django.template.defaulttags.ifequal (   parser,
  token 
)
Outputs the contents of the block if the two arguments equal each other.

Examples::

    {% ifequal user.id comment.user_id %}
        ...
    {% endifequal %}

    {% ifnotequal user.id comment.user_id %}
        ...
    {% else %}
        ...
    {% endifnotequal %}
def google.appengine._internal.django.template.defaulttags.ifnotequal (   parser,
  token 
)
Outputs the contents of the block if the two arguments are not equal.
See ifequal.
def google.appengine._internal.django.template.defaulttags.load (   parser,
  token 
)
Loads a custom template tag set.

For example, to load the template tags in
``django/templatetags/news/photos.py``::

    {% load news.photos %}
def google.appengine._internal.django.template.defaulttags.now (   parser,
  token 
)
Displays the date, formatted according to the given string.

Uses the same format as PHP's ``date()`` function; see http://php.net/date
for all the possible values.

Sample usage::

    It is {% now "jS F Y H:i" %}
def google.appengine._internal.django.template.defaulttags.regroup (   parser,
  token 
)
Regroups a list of alike objects by a common attribute.

This complex tag is best illustrated by use of an example:  say that
``people`` is a list of ``Person`` objects that have ``first_name``,
``last_name``, and ``gender`` attributes, and you'd like to display a list
that looks like:

    * Male:
        * George Bush
        * Bill Clinton
    * Female:
        * Margaret Thatcher
        * Colendeeza Rice
    * Unknown:
        * Pat Smith

The following snippet of template code would accomplish this dubious task::

    {% regroup people by gender as grouped %}
    <ul>
    {% for group in grouped %}
        <li>{{ group.grouper }}
        <ul>
            {% for item in group.list %}
            <li>{{ item }}</li>
            {% endfor %}
        </ul>
    {% endfor %}
    </ul>

As you can see, ``{% regroup %}`` populates a variable with a list of
objects with ``grouper`` and ``list`` attributes.  ``grouper`` contains the
item that was grouped by; ``list`` contains the list of objects that share
that ``grouper``.  In this case, ``grouper`` would be ``Male``, ``Female``
and ``Unknown``, and ``list`` is the list of people with those genders.

Note that ``{% regroup %}`` does not work when the list to be grouped is not
sorted by the key you are grouping by!  This means that if your list of
people was not sorted by gender, you'd need to make sure it is sorted
before using it, i.e.::

    {% regroup people|dictsort:"gender" by gender as grouped %}
def google.appengine._internal.django.template.defaulttags.spaceless (   parser,
  token 
)
Removes whitespace between HTML tags, including tab and newline characters.

Example usage::

    {% spaceless %}
        <p>
            <a href="foo/">Foo</a>
        </p>
    {% endspaceless %}

This example would return this HTML::

    <p><a href="foo/">Foo</a></p>

Only space between *tags* is normalized -- not space between tags and text.
In this example, the space around ``Hello`` won't be stripped::

    {% spaceless %}
        <strong>
            Hello
        </strong>
    {% endspaceless %}
def google.appengine._internal.django.template.defaulttags.ssi (   parser,
  token 
)
Outputs the contents of a given file into the page.

Like a simple "include" tag, the ``ssi`` tag includes the contents
of another file -- which must be specified using an absolute path --
in the current page::

    {% ssi /home/html/ljworld.com/includes/right_generic.html %}

If the optional "parsed" parameter is given, the contents of the included
file are evaluated as template code, with the current context::

    {% ssi /home/html/ljworld.com/includes/right_generic.html parsed %}
def google.appengine._internal.django.template.defaulttags.templatetag (   parser,
  token 
)
Outputs one of the bits used to compose template tags.

Since the template system has no concept of "escaping", to display one of
the bits used in template tags, you must use the ``{% templatetag %}`` tag.

The argument tells which template bit to output:

    ==================  =======
    Argument            Outputs
    ==================  =======
    ``openblock``       ``{%``
    ``closeblock``      ``%}``
    ``openvariable``    ``{{``
    ``closevariable``   ``}}``
    ``openbrace``       ``{``
    ``closebrace``      ``}``
    ``opencomment``     ``{#``
    ``closecomment``    ``#}``
    ==================  =======
def google.appengine._internal.django.template.defaulttags.url (   parser,
  token 
)
Returns an absolute URL matching given view with its parameters.

This is a way to define links that aren't tied to a particular URL
configuration::

    {% url path.to.some_view arg1 arg2 %}

    or

    {% url path.to.some_view name1=value1 name2=value2 %}

The first argument is a path to a view. It can be an absolute python path
or just ``app_name.view_name`` without the project name if the view is
located inside the project.  Other arguments are comma-separated values
that will be filled in place of positional and keyword arguments in the
URL. All arguments for the URL should be present.

For example if you have a view ``app_name.client`` taking client's id and
the corresponding line in a URLconf looks like this::

    ('^client/(\d+)/$', 'app_name.client')

and this app's URLconf is included into the project's URLconf under some
path::

    ('^clients/', include('project_name.app_name.urls'))

then in a template you can create a link for a certain client like this::

    {% url app_name.client client.id %}

The URL will look like ``/clients/client/123/``.
def google.appengine._internal.django.template.defaulttags.widthratio (   parser,
  token 
)
For creating bar charts and such, this tag calculates the ratio of a given
value to a maximum value, and then applies that ratio to a constant.

For example::

    <img src='bar.gif' height='10' width='{% widthratio this_value max_value 100 %}' />

Above, if ``this_value`` is 175 and ``max_value`` is 200, the image in
the above example will be 88 pixels wide (because 175/200 = .875;
.875 * 100 = 87.5 which is rounded up to 88).