This is a custom template filter that allows you to truncate a string to a maximum of num characters, but respecting word boundaries. So, for example, if `string = "This is a test string."`, then `{{ string|truncatechars:12 }}` would give you "This is a..." instead of "This is a te".
Add the line shown, or something similar, to your settings/dev.py, so that you can more clearly see when django is silently hiding errors in your template tags.
A simple template filter for breaking a list into sublists of a given length, you might use this on an ecommerce product grid where you want an arbitrary number of rows of fixed columns. Unlike the other partitioning filters I've seen, this doesn't try to distribute the rows evenly, instead it fills each row for moving onto the next.
This filter preserves the ordering of the input list.
This tag gives you an iterable Python [Calendar object](http://docs.python.org/library/calendar.html) in your template namespace. It is used in the [django-calendar](http://github.com/dokterbob/django-agenda) project.
Use it as follows in your template:
{% get_calendar for <month_number_or_variable> <year_or_variable> as calendar %}
<table>
<tr>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
<th>Sun</th>
</tr>
{% for week in calendar %}
<tr>
{% for day in week %}
<td>{{ day.day }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Default to a static template.
**Example:**
urlpatterns = patterns('',
...
# this rule SHOULD BE the last one
(r'^(?P<template>[a-z-_/]+/?)?$',
'myproj.apps.myapp.views.static_template'),
)
Put it in appname/templatetags/truncatehtml.py and load it with {% load truncatehtml %}, then for instance {{ some_story|truncatehtml:100 }} to truncate the story to 100 characters.
Tags are not counted in the length given, and character entities count as one character.
The filter should never break an open-tag text close-tag sequence without adding in the close tag. It will also preserve character entities. It won't sanitize the HTML, though: garbage in, garbage out.
There's a bit more info about how it works in a [blog post](http://ole-laursen.blogspot.com/2009/05/safe-truncation-of-html.html) I wrote.
The template filter is use for split a string such as "foo|foobar|bar" to select option widget.
You can define the splitter of the string by yourself.
**Usage:**
Add the code into templatetags folder of a installed app, then add below code into your template file.
`
{% load split_as_option %}
<select name="widget_name">
{{ QuerySet.values|split_as_option:"|" }}
</select>
`
Let's say you have a dataset and you want to render a page with sections/subsections/subsubsections down to some arbitrary depth and with arbitrary keys. For example, you might want to show cars broken out by year/price_range or price_range/year or price_range/manufacturer/model. The outliner template tag allows you to support multiple breakdowns of your data in a DRY sort of way.
In order to use it, you supply four things:
1. a template for surrounding each subsection (think turtles all the way down)
2. a queryset (really anything iterable)
3. sectionizer dictionary/objects (see sample code, you must supply key_method)
4. inner html to render the lowest subsections
The template provides the following:
1. It recursively uses each of your sectionizers' key methods to divvy up data sets.
2. It surrounds each section with templates of your choosing.
3. It renders the inner template for all the "leaf" elements of your tree.
4. It supplies some handy context vars for things like depth and outline ids.
What this is not:
1. this is not for arbitrary tree data--think of the tree as fixed depth
2. this is not as simple as the regroup tag--if you have a concrete organization to your data, you should keep it simple and hand-code your templates
I have a need to conditionally format a negative number, a hedgefund's daily price change, Excel style. i.e. show a negative number as a parenthesized number instead of a negative sign. Here is a filter that will do that and more, solving a more general case. See the doctest for examples.
This template filter is meant to insert soft hyphens ([­ entities](http://www.cs.tut.fi/~jkorpela/shy.html)) in text whever it can. For this is relies on a **recent** checkout of the [PyHyphen](http://code.google.com/p/pyhyphen/) interface to the hyphen-2.3 C library, which is also used by Mozilla and OpenOffice.org.
It takes two optional parameters: the language to hyphenate in and the minimum word length to consider for hyphenation. If no language is given, the default language from the settings file is used. The second parameter defaults to 5 characters.
Usage example:
{% load hyphenation %}
{{ object.text|hyphenate:"nl-nl,6" }}
As there is no straight way to re-produce the real tabular inline formsets you get in django-admin, here is how this template has to look like if you do it form your own formsets generated from formset factories.
While working on my website projects today I had the idea to use HTML/JS instead of a IP database to localize the dates and times shown on the websites.
Here are the snippets to use the JS snippets as filters for Django running on Google App Engine. You can use those filters on datetime objects.
If you try to load a template named filename.LANG.html it will try to load filename.de.html first, then filename.html afterwards (assuming that German is the currently selected language).
Usage: Put in a file named langtemplateloader.py under your project, and replace django's default filesystem loader with this in the TEMPLATE_LOADERS section of settings.py:
TEMPLATE_LOADERS = (
'myproject.langtemplateloader.load_template_source',
# 'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
'django.template.loaders.eggs.load_template_source',
)