Easy to use active url highlighting for django
Project description
- info:
Easy to use active URL highlighting for Django
A demo is available on heroku.com
Features
automatic highlighting of currently active <a> tags via CSS class
automatic highlighting of parent <a> tags for menus
removes boring / hardcoded stuff from your life!
Usage
After loading the template library via
{% load activeurl %}
the following code snippet will be rendered like this if request.full_path() starts with /some_page/:
{% activeurl %}
<ul>
<li> <!-- this <li> will render as <li class="active"> -->
<a href="/some_page/">
some_page
</a>
</li>
<li>
<a href="/another_page/">
another_page
</a>
</li>
</ul>
{% endactiveurl %}
Note: The content of {% activeurl %}…{% endactiveurl %} must have valid root tag (i.e. <ul> or <div>, etc) – otherwise an exception will be raised.
Installation
Python 2.6, 2.7, 3.2, 3.3 and PyPy 1.9 are supported.
Install the stable version using pip:
pip install django-activeurl
or install the in-development version using pip:
pip install -e git+git://github.com/hellysmile/django-activeurl#egg=django_activeurl
In your settings.py add the following:
INSTALLED_APPS = ( ... 'django_activeurl', ... ) TEMPLATE_CONTEXT_PROCESSORS = ( ... 'django.core.context_processors.request', ... )
The lxml library is required and thus additional libraries need to be installed to build it:
Ubuntu:
sudo apt-get install libxml2 libxml2-dev libxslt-dev build-essential python-dev sudo ldconfig
Fedora:
sudo yum groupinstall 'Development Tools' sudo yum install libxslt-devel libxml2 libxml2-devel python-devel sudo ldconfig
MacOS X:
brew install libxml2 libxslt sudo update_dyld_shared_cache -force
Windows: A pre-built lxml binary can be found here
Clouds: There’s a 99.99% chance that lxml will build out of the box.
Options
parent_tag =”div|li|self|…” (default: “li”)
parent_tag defines that a parent element – and not the <a> tag itself – should be declared active when there’s a match in URLs. When you need to change the CSS class of the <a> tag, just enter “self”.
css_class =”<string>” (default: “active”)
Defines what CSS class to add to an active element.
Configuration
The default options can be set in settings.py as well:
ACTIVE_URL_KWARGS = {
'css_class': 'active',
'parent_tag': 'li',
'menu': 'yes'
}
ACTIVE_URL_CACHE = True
ACTIVE_URL_CACHE_TIMEOUT = 60 * 60 * 24 # 1 day
ACTIVE_URL_CACHE_PREFIX = 'django_activeurl'
By default django-activeurl will try to retrieve a previously rendered HTML node from Django’s caching backend before active URLs are looked for and a new HTML tree is built. You can disable the cache with ACTIVE_URL_CACHE = False.
In addition, ACTIVE_URL_CACHE_TIMEOUT can be used to define a timeout for keys to expire. The default value is one day.
The last configuration option is ACTIVE_URL_CACHE_PREFIX (which is django_activeurl by default) and defines which name to use in Django’s caching backend.
Tests
pip install tox
tox
Jinja2
Vanilla Jinja2 configuration:
from jinja2 import Environment
from django_activeurl.ext.django_jinja import ActiveUrl
from django_activeurl.ext.utils import options
env = Environment(
extensions=[ActiveUrl]
)
env.globals['options'] = options
{% activeurl options(request, css_class="active", menu="yes", parent_tag="li") %}
<ul>
<li>
<a href="/page/">page</a>
</li>
<li>
<a href="/other_page/">other_page</a>
</li>
</ul>
{% endactiveurl %}
Except for request, options can be omitted.
If you’re using django-jinja, jingo or coffin you need to load the ActiveUrl extension and populate Environment() with options in settings.py.
Background
For building the HTML element tree django-activeurl uses lxml, which is one of the best HTML parsing tools around. More info and benchmarks can be found at habrahabr.ru (in russian). Note that there’s no content rebuilding inside the template tag when no active URLs are found, so there’s no impact on performance.
Credits
initializr is used for the example HTML template.
Simon Whitaker’s »Fork me« ribbon is used in the example.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file django-activeurl-0.1.5.tar.gz
.
File metadata
- Download URL: django-activeurl-0.1.5.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15778e35fc6b91b1df0795961ff5c442663aa79b5f385be39f1b62cf2b048a20 |
|
MD5 | ea573d6697d977a8a9b2003685f7509f |
|
BLAKE2b-256 | ed0924514eb301506d29001e46c992b61d424dcb792b98a8f8d5194b2b4a30f2 |