Extended extends tag that supports passing parameters, which will be made
available in the context of the extended template (and all the way up the
hierarchy).
It wraps around the orginal extends tag, to avoid code duplication, and to
not miss out on possible future django enhancements.
Note: The current implementation will override variables passed from your
view, too, so be careful.
Some of the argument parsing code is based on:
http://www.djangosnippets.org/snippets/11/
Examples:
{% xextends "some.html" %}
{% xextends "some.html" with title="title1" %}
{% xextends "some.html" with title="title2"|capfirst %}
Replaces <code> blocks with syntax highlighted code. Use CSS to actually get the colours you want, look at pygments documentation for extracting css for various styles.
This snippet has the advantage of falling back on <pre> if anything goes wrong, and attempting to guess the syntax of code, falling back on python.
This tag builds on top of the [ifusergroup tag](http://www.djangosnippets.org/snippets/282/), fixes a small bug and introduces support for else blocks.
Meant mostly as a demo of how to do complex block tags, here's a switch/case implementation.
It's deliberately simplistic: no default cases, no multiple values for the same case, etc. This is so that you can focus on the technique.
Pay particular attention to how both switch and case pull out their child nodes and save them for later rendering. This is a very useful technique for these types of "structured" template tags.
Display a random quote. Just add some quotes to the app (you may want to add the administration interface options) and then load the template tag:
`{% load random_quote %}`
and then call the tag to display a random quote from the app
`{% random_quote %}`
feel free to improve it/whatever..
:)
`{% tablify questions name response 8 %}`
Will generate a table structure of a list, ensuring that a
set number of columns is not exceeded.
It takes 4 arguments:
*Context Variable
*Cell Header Attribute
*Cell Data Attribute
*Max Number of Columns
The code will loop through the given variable, creating th and td elements for each item. It will create a new tr when the max number of columns is exceeded.
A couple of utility `Node` subclasses that will automatically cache thier contents.
Use `CachedNode` for template tags that output content into the template:
class SomeNode(CachedNode):
def get_cache_key(self, context):
return "some-cache-key"
def get_content(self, context):
return expensive_operation()
Use `CachedContextUpdatingNode` for tags that update the context:
class AnotherNode(CachedContextUpdatingNode):
# Only cache for 60 seconds
cache_timeout = 60
def get_cache_key(self, context);
return "some-other-cache-key"
def get_content(self, context):
return {"key" : expensive_operation()}
Returns 'Morning', 'Afternoon', or 'Evening' in the local timezone (specified in settings). Required pytz. 0000-1200 considered morning, 1200-1800 considered afternoon, 1800-0000 considered evening.
Cheers to limodou for getting me thinking about this. The only problem with his implementation is that it doesn't support Django's "." syntax for accessing array/dict elements. In the Django style of allowing simple syntax for designers while allowing for greater flexibility, and less template duplication for conditionals that were previously impossible to represent in templates, I modified Django's built-in If tag.
This is an adaptation/enhancement to Django's built in IfNode {% if ... %} that combines if ifequal ifnotequal into one and then adds even more. This
Supports
1. ==, !=
2. not ....
3. v in (1,"y",z)
4. <=, <, >=, >
5. nesting (True and (False or (True or False)))
How to use it:
{% pyif i == 1 or (5 >= i and i != 7) and user.first_name in ('John', 'Jacob') %}
'Tis true.
{% else %}
'Tis false.
{% endif %}
I hope you like it.
The {% widthratio %} template tag is under appreciated! Here, it's combined with CSS to create a bar graphic for the results of an election (this example comes from [this page](http://flickr.com/photos/postneo/405239750/in/photostream/), but has been modified slightly for simplicity's sake).
The widthratio tag can be used to create all sorts of graphs and charts, as well as things like tag clouds. Here, we pass it the number of votes for a candidate, the total number of votes in the election, and the integer 190, which is the width, in pixels, of a "full" bar on the bar graph. In other words, 100% = 190 pixels.
It works great!
**Support good typography! Avoid widows! **
"Widows" are single words that end up on their own line, thanks to automatic line-breaks. This is an no-no in graphic design, and is especially unsightly in headers and other short bursts of text. This filter automatically replaces the space before the last word of the passed value with a non-breaking space, ensuring there is always at least two words on any given line. Usage is like so:
{{ blog.entry.headline|widont }}
It's a simple tag, but good typography is one of the hallmarks of a well-designed site that separates it from amateurish counterparts.
Note: The idea and name "widont" is copped directly from [Shaun Inman](http://shauninman.com), who wrote a [similar feature for WordPress](http://www.shauninman.com/archive/2006/08/22/widont_wordpress_plugin).