OpenBlock 1.2 documentation

templatetags Package

templatetags Package

dateutils Module

Template tags for working with dates. To use these, your template must include:

{% load dateutils %}
ebpub.db.templatetags.dateutils.days_in_month(value)

A filter. Given a datetime.date or datetime.datetime object, returns the number of days in that month.

Example:

{{ some_date|days_in_month }}

Example output:

31

Examples, in python:

>>> import datetime
>>> print days_in_month(datetime.date(2011, 8, 15))
31
>>> print days_in_month(datetime.datetime(2011, 8, 15, 0, 0))
31
>>> print days_in_month(datetime.date(2012, 2, 1))
29
>>> print days_in_month(datetime.date(2011, 2, 1))
28
ebpub.db.templatetags.dateutils.friendlydate(value)

A filter that takes a date and includes 'Today' or 'Yesterday' if relevant, or the day of the week if it's within the past week, otherwise just the date.

Example (in template):

{% start_date|friendlydate %}

Examples, in python:

>>> import mock, datetime
>>> with mock.patch('ebpub.db.templatetags.dateutils.today', lambda: datetime.date(2011, 8, 15)):
...     print friendlydate(datetime.date(2011, 8, 15))
...     print friendlydate(datetime.date(2011, 8, 16))
...     print friendlydate(datetime.date(2011, 8, 14))
...     print friendlydate(datetime.date(2011, 8, 13))
...     print friendlydate(datetime.date(2011, 8, 9))
...     print friendlydate(datetime.date(2011, 8, 8))
...
Today August 15, 2011
Tomorrow August 16, 2011
Yesterday August 14, 2011
Saturday August 13, 2011
Tuesday August 9, 2011
August 8, 2011

eb Module

Template tags for working with NewsItem, Location, etc.

To use these, your template must include:

{% load eb %}
ebpub.db.templatetags.eb.METRO_NAME()

Prints the metro_name from get_metro(), titlecase.

Example:

<h1>{% METRO_NAME %}</h1>
ebpub.db.templatetags.eb.contains(value, arg)

Filter to check whether arg is in value. Obsolete since Django 1.2, use the 'in' operator instead.

Example:

{% if "Bob" in name_list %}
   Hi Bob!
{% endif %}
ebpub.db.templatetags.eb.featured_lookup_for_item(context, newsitem, attribute_key)

Tag that, given a NewsItem, finds any "Featured" Lookups that correspond to its values for the named attribute. Saves them in the current context under the same name as the attribute.

Example:

{% featured_lookup_for_item item 'tags' %}
{% for tag in tags %}
  This item has tag {{ tag }}
{% endfor %}

Get all "Featured" Lookups names and URLs for them; puts in the context as 'featured_lookups', a mapping grouped by schema.

Example:

{% get_featured_lookups_by_schema %}
{% for schema, lookups in featured_lookups.items %}
   <ul>{{ schema }}
    {% for info in lookups %}
      <a href="{{ info.url }}">{{ info.lookup }}</a>
       ...
    {% endfor %}
{% endfor %}
ebpub.db.templatetags.eb.get_locations_for_item(parser, token)

Tag that puts into the context some data about intersecting locations, useful for eg. linking to URLs based on those locations.

Syntax:

{% get_locations_for_item newsitem location_type_slug (location_type_slug2 ...) as varname %}

The location_type_slug arguments will be used, in the order given, to specify which types of locations to find.

The last argument is the name of the context variable in which to put the result.

For each matching location, the result will contain a dictionary with these keys: location_slug, location_name, location_type_slug, location_type_name.

Here's an example template in which we build links for each intersecting location:

{% for item in news_items %}
  {% get_locations_for_item item 'village' 'town' 'city' as locations_info %}
  {% for loc_info in locations_info %}
    <li><a href="http://example.com/yourtown/{{loc_info.location_slug}}">
       Other News in {{ loc_info.location_name }}
    </a></li>
  {% endfor %}
{% endfor %}
ebpub.db.templatetags.eb.get_metro(context)

Tag that puts get_metro() into the context as METRO

Example:

{% get_metro %}
<p>Current metro is {{ METRO.city_name }}</p>
ebpub.db.templatetags.eb.get_metro_list(context)

Tag that puts settings.METRO_LIST into the context as METRO_LIST.

Example:

{% get_metro_list %}
{% for metro in METRO_LIST %}
  <p>The metro is {{ metro.city_name }}</p>
{% endfor %}
ebpub.db.templatetags.eb.get_newsitem(parser, token)

Tag that puts a newsitem with the given ID in the context with the given variable name. Examples:

{% get_newsitem some_id as my_item %}
{% get_newsitem '23' as my_other_item %}
<p>{{ my_item.title }}</p>
<p>{{ my_other_item.title }}</p>
ebpub.db.templatetags.eb.get_newsitem_list_by_attribute(parser, token)

Tag that gets a list of NewsItems with a given attribute value, and saves it in a context variable. Optionally exclude a NewsItem by id. (Useful if you have a NewsItem and you want to build a list of similar NewsItems without including the one you already have.)

Syntax:

{% get_newsitem_list_by_attribute [schema] [newsitem_to_ignore] [att_name]=[value_or_var_containing_value] as [context_var] %}
{% get_newsitem_list_by_attribute [schema] [att_name]=[value_or_var_containing_value] as [context_var] %}

The schema and newsitem_to_ignore arguments can be either IDs or instances of Schema and NewsItem, respectively. Schema can also be specified by slug.

Example 1. Here's a list of the latest 3 items that have the "tag" value of "garage sale" and schema slug "local-news":

{% get_newsitem_list_by_attribute "local-news" tag="garage sale" as recent_sales %}
{% for sale in recent_sales|slice:":3" %}
   <li><i>{{ sale.title }}</i></li>
{% endfor %}

Example 2. Here's a list of items that have the same "business_id" value as item does. item itself is excluded from the list.

{% get_newsitem_list_by_attribute item.schema item business_id=item.attributes.business_id as other_licenses %}
{% for item in other_licenses %}
   <li><i>{{ item.title }}</i>
{% endfor %}
ebpub.db.templatetags.eb.greaterthan(value, arg)

Filter. Obsolete since Django 1.1: Use the > operator instead.

ebpub.db.templatetags.eb.isdigit(value)

Filter that returns whether the value is a digit.

Example:

{% if "123"|isdigit %} It's a digit {% endif %}
{% if not "Fred"|isdigit %} It's not a digit {% endif %}

Python examples:

>>> isdigit("1")
True
>>> isdigit("999")
True
>>> isdigit("42.1")
False
>>> isdigit("hello")
False
>>> isdigit("")
False
ebpub.db.templatetags.eb.json_lookup_values_for_attribute(schema_slug, sf_name)

Given a schema slug and attribute name, returns all the current Lookup values of the relevant attribute, as a JSON-formatted list.

Assumes the relevant schemafield has is_lookup=True.

Example:

<script>
 var lookups = {% json_lookup_values_for_attribute 'police-reports' 'violations' %};
</script>

Example output:

<script>
 var lookups = ['burglary', 'speeding', 'vandalism'];
</script>
ebpub.db.templatetags.eb.lessthan(value, arg)

Filter. Obsolete since Django 1.1: Use the < operator instead.

ebpub.db.templatetags.eb.newsitem_list_by_schema(parser, token)

Tag that renders a NewsItem, or list of NewsItems, using the appropriate newsitem_list template, optionally grouped by schema.

Syntax:

{% newsitem_list_by_schema [newsitem_or_newsitem_list] [ungrouped?] %}

Examples:

{% newsitem_list_by_schema newsitem "ungrouped" %}
{% newsitem_list_by_schema newsitem_list %}
ebpub.db.templatetags.eb.safe_id_sort(value, arg)

Filter that sorts like Django's built-in "dictsort", but sorts second by the ID attribute, to ensure sorts always end up the same.

Example:

{% for item in itemlist|safe_id_sort "item_date" %} ... {% endfor %}
ebpub.db.templatetags.eb.schema_plural_name(schema, value)

Tag that shows singular or plural name of a schema, depending on value. Example:

{% schema_plural_name schema 3 %}  --> Restaurant Inspections
{% schema_plural_name schema 1 %}  --> Restaurant Inspection
ebpub.db.templatetags.eb.search_placeholder(parser, token)

Tag that stores in a context variable a list of ebpub.db.models.LocationType, plus 'address', with an optional prefix.

Useful for search form widgets where we want to use this as placeholder text on the input, and need to write exactly the same string over and over because javascript will be checking for the placeholder text, restoring it, etc.

Example:

{% set_search_placeholder "Some prefix" as some_var %}
<p>{{ some_var }}</p>

If you have LocationTypes named 'ZIP' and 'neighborhood,' this would output in the template:

<p>Some prefix address, ZIP, or neighborhood</p>

eb_filter Module

Template tags mostly related to the ebpub.db.views.schema_filter() view: generating URLs and forms to link/submit to that view.

To use these, your template must include:

{% load eb_filter %}
ebpub.db.templatetags.eb_filter.filter_form_inputs(parser, token)

Template tag that takes same args as filter_url(), but outputs a set of hidden form inputs encapsulating the current filter chain, optionally with added or removed filters.

For example, to make a form that preserves current filters except allows choosing a new date range, you would do:

<form action="{% filter_url filters.schema %}">
   {% filter_form_inputs filters -"date" %}
   <input type="text" name="start_date">
   <input type="text" name="end_date">
   <input type="submit">
</form>

In this example, the form action URL is generated by filter_url with just the schema, and all non-date current filters are inserted by using filter_form_inputs on the second line. The new dates are given by normal non-hidden form fields.

ebpub.db.templatetags.eb_filter.filter_url(parser, token)

Template tag that outputs a URL based on the filter chain, with optional additions/removals of filters. The first argument is required and can be either an existing FilterChain or a Schema:

{% filter_url filter_chain %}
{% filter_url schema %}

To remove a NewsitemFilter from the url, specify the key with a leading "-":

{% filter_url filter_chain -key_to_remove %}
{% filter_url filter_chain -"key_to_remove" %}
{% filter_url filter_chain -key1 -key2 ... %}

To add NewsitemFilters to the url, specify the key with a leading "+", followed by args to use for constructing a NewsitemFilter.

Keys and values will be passed to FilterChain.add(); see its docs for info on legal values. But briefly, the keys are either SchemaField instances, or a string understood by SchemaFilter, such as 'pubdate':

{% filter_url filter_chain +"key" value %}
{% filter_url filter_chain +key value1 value 2 ... %}
{% filter_url filter_chain +key1 "arg1a" "arg1b" +key2 "arg2a" ... %}

You can even mix and match additions and removals:

{% filter_url filter_chain -key1 +key2 arg2 -key3 +key4 arg4 ... %}

eb_json Module

Custom template tags for dealing with json.

To use these, your template must include:

{% load eb_json %}
ebpub.db.templatetags.eb_json.json_value(value, arg=None)

Filter that turns a JSON string into a data structure.

Example:

{% for item in "[1,2,3]"|json_value %}
  <li>{{ item }}</li>
{% endfor %}

This would insert into the page:

<li>1</li>
<li>2</li>
<li>3</li>

mapping Module

Template tags for helping with maps. To use these, your template must include:

{% load mapping %}
ebpub.db.templatetags.mapping.map_icon_img(obj)

Returns an image tag with a URL for a map icon for the given object, which may be a db.Schema, or streets.PlaceType.

(If there's no image configured but there's a fill color, makes a little box of the right color.)

Example:

{% map_icon_img [schema] %}
{% map_icon_img [place_type] %}

raw Module

To use these, your template must include:

{% load raw %}
ebpub.db.templatetags.raw.raw(parser, token)

Whatever is between {% raw %} and {% endraw %} will be preserved as raw, unrendered template code. If 'silent' is passed in -- {% raw silent %} -- then the resulting output will not contain the {% raw %} and {% endraw %} tags themselves. Otherwise, the output will include an {% endraw %} at the start and {% raw %} at the end, so that other parts of the page aren't vulnerable to Django template escaping injection.

Probably only useful for development.

Example:

{% raw %}
   Some django template code, like {{ something }}.
{% endraw %}

Example output:

Some django template code, like {{ something }}.