Login

Quick-and-dirty decorator for static navigation context

Author:
pablo_PXL
Posted:
January 30, 2011
Language:
Python
Version:
1.1
Score:
1 (after 1 ratings)

Ah... the quick-and-dirty decorator for static navigation context, such a time-saver ;)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
## helpers.py
# Mark as active helper
def mark_as_active_section(col, current_section):
    def mark_as_active(item, current_section):
        if current_section.startswith(item['id']):
            item['active'] = True
        return item
    return map(lambda i: mark_as_active(i, current_section),col)

# Is section decorator
def is_section(func):
    def section_marker(*args, **kwargs):
        if kwargs['menu']:
            kwargs['menu'] = mark_as_active_section(kwargs['menu'], func.__name__)
        return func(*args, **kwargs)
    return section_marker

## urls.py
# Static Menu
menu = lambda: [
    {'id':'home','title':'Home','active':False,'href':'/home/'},
    ...
]

urlpatterns = patterns('',
    (r'^home/$', main_views.home, {'menu':menu()}),
   ...
)

## views.py
from helpers import is_section

@is_section
def home(request, menu):
    context = Context({'menu':menu,...})
    ...

## templates/start.html
{% for section in menu %}
    <li><a href="{{ section.href }}" {% if section.active %} class="active" {% endif %}>
          {{ section.title }}
    </a></li>
{% endfor %}

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.