Two template tags (I keep them in an app called "utils") handy for building menus out of flatpages. I only have two levels of hierarchy, and the frontpage is dynamic, so that's what it does. There is some flexibility, however, in that you can change the regex that defines a "root" page.
You can pass it the flatpage object that is normally given to a flatpage template, or you can pass it a string (for any other page).
Deliberatley does not create any markup -- flexibility is key, so it adds the list of root urls or child urls as a name in the current context. You get to pick the name.
Please visit the [GitHub archive](http://wiki.github.com/0sn/nameremoved/flatpages) where i keep this up to date, there's a better explanation and you can see it in use.
This module defines a template tag `{% ifactive %}` that lets you determine which page is currently active. A common use case is for highlighting the current page in a navigation menu.
It uses the same syntax for specifying views as the `{% url %}` tag, and determines whether a particular page is active by checking if the same view is called with the same arguments, instead of just comparing URLs. As a result, it can handle cases where different URLs map to the same view.
Example usage:
<a {% ifactive request page1 %}class='active'{% endifactive %}
href='{% url page1 %}'>Page 1</a>
<a {% ifactive request page2 %}class='active'{% endifactive %}
href='{% url page2 %}'>Page 2</a>
...
<a {% ifactive request pageN %}class='active'{% endifactive %}
href='{% url pageN %}'>Page N</a>
It also can be extended to further reduce the amount of repetitive code. For instance, you could write a template tag that has class='active' as the block contents, and always gets the variable from context['request']:
def do_activeif(parser, token):
"e.g. <a {% activeif page1 %} href='{% url page1 %}'>Page 1</a>"
tag_args = token.contents.split(' ')
view_name = tag_args[1]
args, kwargs = _parse_url_args(parser, tag_args[2:])
return ActiveNode('request', view_name, args, kwargs, NodeList(TextNode('class="active"')))
register.tag('activeif', do_activeif)
Note that you will have to add the ActiveViewMiddleware to your settings.py:
MIDDLEWARE_CLASSES = (
...,
'path.to.this.module.ActiveViewMiddleware'
)
You may also want to add this template tag as a built-in, so you don't have
to call `{% load %}`. In somewhere that's imported by default (e.g. where you
define your views), add:
from django import template
template.add_to_builtins('path.to.this.module')
This is a custom block tag and is used like this:
{% load whitespaceoptimize %}
{% whitespaceoptimize "css" %}
/* CSS comment */
body {
color: #CCCCCC;
}
{% endwhitespaceoptimize %}
And when rendered you get this output:
body{color:#CCC}
To install it, download the snippet and call it `myapp/templatetags/whitespaceoptimize.py`. (Make sure you have a `__init__.py` in the `templatetags` directory)
You need to download and install [slimmer](http://www.issuetrackerproduct.com/Download#slimmer) and put on your Python path.
The block tag can be used for `javascript` and `html` as well as `css`. You can also let it guess the format; this would work for example:
{% whitespaceoptimize %}
<table>
<tr> ...
{% endwhitespaceoptimize %}
...but this would just replicate the functonality of the [built-in spaceless tag](http://docs.djangoproject.com/en/dev/ref/templates/builtins/#spaceless)
Works much like an inclusion_tag but allows the template_name to be given as an argument or defaults to partials/MODELNAME.html where MODELNAME is 'got' from the context object you want to render. Its very rough so improvements very welcome!
It would be nice to be able to pass new context variables as template tag [keyword] arguments for use in the template to be rendered so you basically have a template tag equivalent for render_to_string...
Example usage in a template:
{% render_partial post %} or {% render_partial post partials/super_post.html %}
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 %}
Uses JSMin. Python version available from
[http://www.crockford.com/javascript/jsmin.py.txt](http://www.crockford.com/javascript/jsmin.py.txt)
Provides template tags to minify JavaScript on the fly.
`{% minifyjs %}[code]{% endminifyjs %}`
This template tag does two things needed to display content from something like a TextField wrapped with TinyMCE in Flash's htmlText component:
1. Replace `<p>` tags with `<br />`
2. Strip all occurances of `\n`
This tag allow you to use C-like switch tag in your templates.
It is useful for sequencial and repetitive `{% ifequal %}` tags.
To install it in your project, you just need to follow [these instructions](http://www.djangoproject.com/documentation/templates_python/#extending-the-template-system)
Here is a Django template tag that allows you to create complex variables specified in JSON format within a template.
It enables you to do stuff like:
{% var as person %}
{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
"212 555-1234",
"646 555-4567"
]
}
{% endvar %}
<p>{{person.firstName}}, </br>
{{person.address.postalCode}}, </br>
{{person.phoneNumbers.1}}
</p>
This tag also enables me to do dynamic CSS using as follows:
# urlpatters
urlpatterns = patterns('',
(r'^css/(?P<path>.*\.css)$', 'get_css'),
)
#views
def get_css(request, path):
return render_to_response('css/%s' % path, {},
mimetype="text/css; charset=utf-8")
# dynamic css within in /path/to/app/templates/css'
{% load var %}
{% var as col %}
{
"darkbg": "#999",
"lightbg": "#666"
}
{% endvar %}
{% var as dim %}
{
"thinmargin": "2em",
"thickmargin": "10em"
}
{% endvar %}
body {
background: {{col.darkbg}};
margin: {{dim.thinmargin}};
}
Usage: (if you save it as pigmentation.py as I did)
{% load pigmentation %}
{% autoescape off %}
{{ somevariable|pygmentize }}
{% endautoescape %}
There already a few of this code around, but this one is pretty clean, and includes css. It also works in both the development server and Dreamhost (python2.4 in my django config) without any unicode problems.
Just add it in templatetags/delicious.py
In your template:
<h3>Del.icio.us</h3>
<ul class="list">
{% load delicious %}
{% load_delicious_links %}
{% for link in delicious_links %}
<li><a href="{{link.link}}">{{link.title|safe}}</a></li>
{% endfor %}
</ul>