Latest Version: 0.9.6.2
  Dashboard > Pylons Cookbook > ... > Templating > Menu Accesskeys Automation
  Pylons Cookbook Log In | Sign Up   View a printable version of the current page.  
  Menu Accesskeys Automation
Added by Graham Higgins, last edited by James Gardner on Apr 07, 2007  (view change)
Labels: 
(None)

Name Space Section Page Version Status Curator Reviewed Author(s)
Menu Accesskeys Automation Pylons CookBook   Menu Accesskeys Automation 1.0 Draft Graham Higgins False  

Menu Accesskeys Automation

This is a small code recipe for automating the genaration of accesskeys for a menu (because of i18n), for example your site's main navigation bar. It will start using the first char of each item in the menu list, if that access key is already used, then it tries to use the second char, and so forth. If there are no more available access keys, then none is used. For the generated menu links that use an accesskey, the corresponding char within the link's text will be underlined.

Now, some code; our example menu list:

1
2
3
4
5
6
menu = \[
    (h._('Ho'), h.url_for(controller='index', action='index')),
    (h._('Ho'), h.url_for(controller='index2', action='index')),
    (h._('HO'), h.url_for(controller='index3', action='index'))
]
c.menu = menu

We've added the menu to the c object because the rest will be done inside a Myghty template.

Now the Myghty template in question:

 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
44
45
46
47
48
49
50
51
52
53
54
55
56
<div id="menu">
%   if 'REMOTE_USER' in request.environ:
    <ul>
%      for link, url, key in c.menu:
%           if toggle.strip() == link:
        <li class="active">
%               if key is not None:
            <% h.link_to(
                        link.replace(key, h.content_tag('u', key)),
                        url,
                        class_='active',
                        accesskey=key) %>
%               else:
            <% h.link_to(link, url, class_='active') %>
%               # endif
        </li>
%           else:
        <li>
%               if key is not None:
            <% h.link_to(link.replace(key, h.content_tag('u', key)), url, accesskey=key) %>
%               else:
            <% h.link_to(link, url) %>
%               # endif
        </li>
%   #       endif
%   #   endfor
%   # endif
    </ul>
</div>

<%init>
    def create_i18n_menu():
        keys_list = {}
        menu = []
        for name, url in c.menu:
            for n in range(len(name)):
                if name[n].upper() not in \[x.upper() for x in keys_list.values()]:
                    keys_list\[url] = name[n]
                    break
                else:
                    n += 1
            else:
                keys_list\[url] = None
            menu.append((name, url, keys_list\[url]))
        return menu

    cache = m.get_cache()
    if not cache.has_key('i18n_menu'):
        cache.set_value('i18n_menu', create_i18n_menu(), expiretime=60)
    c.menu = cache.get_value('i18n_menu', type='memory',
                            createfunc=create_i18n_menu, expiretime=60)
</%init>

<%args>
toggle
</%args>

The above will generate the following html:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<div id="menu">
    <ul>
        <li>
            <a href="/index" accesskey="H"><u>H</u>o</a>
        </li>
        <li>
            <a href="/index2" accesskey="o">H<u>o</u></a>
        </li>
        <li>
            <a href="/index3">HO</a>
        </li>
    </ul>
</div>

And the source html:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<div id="menu">
    <ul>
        <li>
            <a href="/index" accesskey="H"><u>H</u>o</a>
        </li>
        <li>
            <a href="/index2" accesskey="o">H<u>o</u></a>
        </li>
        <li>
            <a href="/index3">HO</a>
        </li>
    </ul>
</div>

The first link will be accessed by ALT+h, the seccond ALT+o and the last does not have an accesskey because there was no more letters available.

Of course you will have to adapt this code to your own, because these are just parts of an application I'm building.

Have fun!

Site running on a free Atlassian Confluence Open Source Project License granted to Pylons. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.3.3 Build:#645 Feb 13, 2007) - Bug/feature request - Contact Administrators
Top