4. Extra template tags

portal.utils.templatetags.datetimeformatting.comparetime(value, arg)

Compares date to see if it is newer

Usage:

{% load i18n formatting datetimeformatting %}
{% if item|getMetadataFieldValueByName:"created"|
   datetimeobject|comparetime:user.last_login %}
portal.utils.templatetags.datetimeformatting.date_back(parser, token)

Works out the date. Generally not used This template tag is to return a date based upon a value given of how many Days back to go.

portal.utils.templatetags.datetimeformatting.dateobject(value, parse_values)

Returns a Python datetime object back from a string, taking into account the timezone that the date is presented in and the timezone for Portal. _dt could be returned without doing the timezone translation if needed.

Usage:

{% load i18n formatting datetimeformatting %}
{{ job.getStarted|dateobject:"%b %d %Y"|date:"DATE_FORMAT" }}
portal.utils.templatetags.datetimeformatting.datetimeobject(value)

Returns a Python datetime object back from a string, taking into account the timezone that the date is presented in and the timezone for Portal. _dt could be returned without doing the timezone translation if needed.

If the value cannot be parsed returns the original value as a string.

Usage:

{% load i18n formatting datetimeformatting %}
{{ job.getStarted|datetimeobject|date:"DATETIME_FORMAT" }}
portal.utils.templatetags.datetimeformatting.is_ISODate_past(value)

Compares dates with Vidispine

Takes a ISO8601 date says whether it has passed or is in the future with now.

If the date is not available, then return true.

portal.utils.templatetags.datetimeformatting.timeuntilremoval(value)

Returns a Python datetime object back from a string, of how many hours left until this item is about to be deleted

Usage:

{% load i18n formatting datetimeformatting %}
{{ job.getStarted|timeuntilremoval|date:"DATETIME_FORMAT" }}
portal.utils.templatetags.docstring.docstring(parser, token)

Ignores the contents, but useful to be parsed out by documentation parser Ignores everything between {% docstring %} and {% enddocstring %}.

Useful for documenting templates

Formatting template tags for formatting forms and text fields

class portal.utils.templatetags.formatting.VerbatimNode(text)

Verbatim tag for allowing the rendering of JQuery template tags such as that used by mustache.js.

Wrap {% verbatim %} and {% endverbatim %} around those blocks of jQuery templates and this will try its best to output the contents with no changes.

render(context)

Return the node rendered as a string.

portal.utils.templatetags.formatting.display_value(field)

Returns the displayed value for this BoundField, as rendered in widgets.

Usage::
{% load formatting %} {{ field|display_value }}
portal.utils.templatetags.formatting.editable_type(field)

We match the types to the types that Edit in place supports.

portal.utils.templatetags.formatting.editable_widgettype(field)

Other possible values: CheckboxInput, SelectMultiple, CheckboxSelectMultiple,TextInput, HiddenInput, Textarea

portal.utils.templatetags.formatting.field_options(field)

Returns the options from a field

portal.utils.templatetags.formatting.field_type(value)

Returns the type of a field in a form.

portal.utils.templatetags.formatting.field_value(field)

Returns the value for this BoundField, as rendered in widgets.

portal.utils.templatetags.formatting.framesToSMPTE(value, arg)

Convert from frames to SMTPE string Takes FPS

portal.utils.templatetags.formatting.get_file_extension_from_path(value)

Returns the file extension when given a path

portal.utils.templatetags.formatting.get_filename_from_path(value)

Returns a filename when given a path

portal.utils.templatetags.formatting.secondsToSMPTE(value, arg)

Convert from seconds to SMTPE string Takes FPS

portal.utils.templatetags.formatting.space_replace(value, arg)

Replaces an occurance of a string with a space for instance: “_” ” ” {{ somevariable|space_replace:”_” }}

portal.utils.templatetags.formatting.split_form(form, arg)

Split up a form in to component parts:

<form action="{% url 'thanks' slug %}" method="post">
<table>
    {{ form|split_form:":2" }}
    <tr>
        <td colspan="2">{% trans 'I am particularly interested in' %}:
        </td>
    </tr>
    {{ form|split_form:"2:" }}
    <tr><th></th>
        <td><input type="submit" value="{% trans 'Submit' %}"
              class="submit-button">
        </td>
   </tr>
</table>
</form>
portal.utils.templatetags.formatting.strip_extension(value)

Strips the file extension when given a path

portal.utils.templatetags.formatting.truncchar(value, arg)

Middle truncating after a certain number of characters

portal.utils.templatetags.formatting.widget_type(value)

Returns the widget type for a field in a form.

General template tags

portal.utils.templatetags.general.dictKeyLookup(the_dict, key)

Returns a value from a dict.

Usage:

{{ dict|dictKeyLookup:key }}
portal.utils.templatetags.general.formatToBits(bits)

Format size into bits:

Usage:

{{ shape_tag.tag.getAudioBitrate|formatToBits }}
portal.utils.templatetags.general.formatToBytes(dummy, bytes)

Format size from bytes into human readable amount

Usage:

{{ ""|formatToBytes:f.getSize }}
portal.utils.templatetags.general.formatToTime(dummy, seconds)

Format seconds to time.

Usage:

{{ ""|formatToTime:systemmetadata.durationSeconds }}
portal.utils.templatetags.general.getUnit(dummy, bytes)

Round up a filesize to present it in the proper size.

Symbol Bytes
tb 1099511627776
gb 1073741824
mb 1048576
kb 1024
b < 1024

Usage:

getUnit(null, bytes)
portal.utils.templatetags.general.get_range(value)

Filter - returns a list containing range made from given value Usage (in template):

<ul>{% for i in 3|get_range %}
<li>{{ i }}. Do something</li>

{% endfor %}</ul>

Results with the HTML: <ul>

<li>0. Do something</li> <li>1. Do something</li> <li>2. Do something</li>

</ul>

Instead of 3 one may use the variable set in the views

portal.utils.templatetags.general.listLookup(the_list, index)

Returns a specific element from a list.

Usage:

{{ l|listLookup:list }}
portal.utils.templatetags.getstring.qstring(parser, token)

Get the current request’s query string from within the page.

USAGE::
{% qstring %} or {% qstring as current_qstring %}
portal.utils.templatetags.getstring.qstring_del(qstring, del_qstring)

Returns a query string w/o some keys, every value for each key gets deleted.

More than one key can be specified using an & as separator.

Usage:

{{ my_qstring|qstring_del:"key1&key2" }}
portal.utils.templatetags.getstring.qstring_get(qstring, key)

Given a key, the value is returned from the query string

Usage:

{{ my_qstring|qstring_get:"key1" }}
portal.utils.templatetags.getstring.qstring_set(qstring, set_qstring)

Updates a query string, old values get deleted.

Usage:

{{ my_qstring|qstring_set:"key1=1&key1=2&key2=3" }}

UI messages into templates

class portal.utils.templatetags.messages.GrowlMessagesNode(messages)

Outputs grouped Django Messages Framework messages for formatting into JQuery Growl messages

render(context)

Return the node rendered as a string.

class portal.utils.templatetags.messages.MessagesNode(messages)

Outputs grouped Django Messages Framework messages in separate lists sorted by level.

render(context)

Return the node rendered as a string.

portal.utils.templatetags.messages.render_growl_messages(parser, token)

Render Growl Messages. Use this tag somewhere where we can output the Javascript messages.

Usage:

{% render_growl_messages messages %}

Switch support

portal.utils.templatetags.switch.do_switch(parser, token)

The {% switch %} tag compares a variable against one or more values in {% case %} tags, and outputs the contents of the matching block. An optional {% else %} tag sets off the default output if no matches could be found:

{% switch result_count %}
    {% case 0 %}
        There are no search results.
    {% case 1 %}
        There is one search result.
    {% else %}
        Jackpot! Your search found {{ result_count }} results.
{% endswitch %}

Each {% case %} tag can take multiple values to compare the variable against:

{% switch username %}
    {% case "Jim" "Bob" "Joe" %}
        Me old mate {{ username }}! How ya doin?
    {% else %}
        Hello {{ username }}
{% endswitch %}

url_optional is exactly the same as from django.template.defaulttags.url EXCEPT kwargs equal to None are removed this allows a bit more flexibility than the use of {% url %} where nesting is rested on optional base kw arguments.

see http://code.djangoproject.com/ticket/9176

class portal.utils.templatetags.url_optional.URLNodeOptional(view_name, args, kwargs, asvar)

identical to django.template.defaulttags.URLNode but removes kwargs equal to None before resolving the url

render(context)

Return the node rendered as a string.

portal.utils.templatetags.url_optional.url_optional(parser, token)

creates the default URLNode, then routes it to the Optional resolver with the same properties by first creating the URLNode, the parsing stays in django core where it belongs.

class portal.utils.templatetags.videopreview.SequencePlayerNode(item)

Get the sequence information and then render the previewsequence.html template.

render(context)

Return the node rendered as a string.

portal.utils.templatetags.videopreview.build_videoitem_preview_map(numberOfKeyFrames, numToShow)

We need to get the number of keyframes and then return a dictionary that can be used to build up the correct number of keyframes, or build up the template. We use this as a filter, so that it can be passed back into the current template

So we pass in the number of keyframes > 60, we then see how often to create the keyframes..

Usage for 6 keyframes:

{% for keyframe in item.num_of_keyframes|videoimagemap:"6"  %}
    <area shape="rect"
          coords="{{ keyframe.0 }},0,{{ keyframe.1 }},150"
          href="{% url 'mediaviewdetail' item.slug,item.guidstring %}"
          onmouseover="mediaitem{{ forloop.parentloop.counter }}.src=                    '{{ STATIC_URL }}{{ item.get_keyframe_path }}
            {{ keyframe.2|stringformat:"03d" }}.jpg'" alt="" />
{% endfor %}
portal.utils.templatetags.videopreview.previewplayer(parser, token)

Based upon the content type of the current item, as sent through with tag decide on what player to utilise.

The template includes pluginblock MediaViewVideoPlayerOptionsJS for customising video player options. Please see https://github.com/Cantemo/PortalPluginTemplate/blob/master/video_player_options.py for examples.

Args:
  • item - the item so that we can find out the type and what preview it has.
portal.utils.templatetags.videopreview.sequenceplayer(parser, token)

Build a sequence player for a given item.

Args:
  • item - the item should be a sequence (check with isSequence).

4.1. preview_by_mime_type

This tag will let you pass in a previews list, a mimetype and will return an absolute URI with an address of the media of that type. If the replace_urls config option has been set with an IP address or hostname then it will honour that hostname, otherwise it will user SERVER_NAME and SERVER_PORT to work out the current request and create a URI using that:

{% load vidispinetags %}
{% preview_by_mime_type previews "video/mp4" %}

This is ideally used when using a flash player, that needs an absolute URI to the media, relative URLS do not always work.

See also:

  • _paginator
  • _plugins-template
  • _thumbnail-configuration