Generate menu

20 views
Skip to first unread message

annet

unread,
Sep 24, 2008, 7:18:17 AM9/24/08
to web2py Web Framework
I have some questions about generating a menu.

Am I right, when I conclude that when I want to generate an
application wide menu, I have to define all functions in a single
(e.g. the default) controller?


From the documentation I understand that there have various options to
generate a menu:

In the controller:

# 1) From examples default.py #

app=request.application
response.menu=[
['home',request.function=='index','/%s/default/index'%app],
['download',request.function=='download','/%s/default/
download'%app],
['features',request.function=='features','/%s/default/
features'%app],
['api',request.function=='api','/%s/default/api'%app],
['dal',request.function=='orm','/%s/default/orm'%app],
['examples',request.function=='examples','/%s/default/
examples'%app],
['license',request.function=='license','/%s/default/license'%app],
['thanks',request.function=='thanks','/%s/default/thanks'%app],
['faq',request.function=='faq','/%s/default/faq'%app]]


# 2) From examples database_examples.py #

response.menu=[['Register
User',False,URL(r=request,f='register_user')],
['Register Dog',False,URL(r=request,f='register_dog')],
['Register
Product',False,URL(r=request,f='register_product')],
['Buy product',False,URL(r=request,f='buy')]]


# 3) From the cookbook pdf defined within the function #

response.menu=[['civilized',False,URL(r=request,f='civilized')],
['slick',True,URL(r=request,f='slick')],
['basic',False,URL(r=request,f='basic')]]


# 4) From admin's default.py #

_f=request.function
response.menu=[(T('site'),_f=='site','/%s/default/
site'%request.application)]
if request.args:
_t=(request.application,request.args[0])
response.menu.append((T('about'),_f=='about','/%s/default/about/
%s'%_t))
response.menu.append((T('design'),_f=='design','/%s/default/design/
%s'%_t))
response.menu.append((T('errors'),_f=='errors','/%s/default/errors/
%s'%_t))
if not session.authorized: response.menu=[(T('login'),True,'')]
else: response.menu.append((T('logout'),False,'/%s/default/
logout'%request.application))
response.menu.append((T('help'),False,'/examples/default/index'))

(Could one of you please explain this code to me?)


And 5) in the view using an unordered list with list items.


I wonder what is the best way to create a menu, taking a single point
of definition into account. Furthermore, how do these menus reflect
their state, that is, active - inactive - hover?


Best regards,

Annet.





mdipierro

unread,
Sep 24, 2008, 8:42:41 AM9/24/08
to web2py Web Framework
This is a convention, not a rule. Since there is nothing in web2py
that make menus. There is something T2 instead.

response.menu=[
item1,
item2,
item3,
]

where item=[label, active, link]
for example

response.menu=[
['Home',False,URL(r=reuqest,f='index')],
]

i.e. 'Home' is the menu label, False means the menu item is not
selected and URL(....) builds the URL to the action.

You can display this in any view with, for example.

<ul class="menu">
{{for _item in response.menu or []:}}
<li{{if _item[1]:}} class="selected"{{pass}}><a
href="{{=_item[2]}}>{{=_item[0]}}</a></li>
{{pass}}
</ul>

In T2 you can also have nested menus

response.menu=[
[label, active, link, [[sub',False,other_link],]],
]

and in your view you simply

{{=t2.menu()}}

Massimo

annet

unread,
Sep 30, 2008, 3:11:38 AM9/30/08
to web2py Web Framework
Massimo,

Thanks for your explanation. It raised a question, though, what is T2,
and where is it documented.


Best regards,

Annet

yarko

unread,
Sep 30, 2008, 9:48:42 AM9/30/08
to web2py Web Framework
Annet-

T2 is Massimo's work to put a higher level of abstraction on web2py.
It's also been a way to work out how modules will work in web2py.

You can find it (and the current document and some simple examples) at
http://launchpad.net/t2

You can get it using bazaar (http://bazaar-vcs.org/). You can search
this list, but I think it's best to refer to launchpad.

Yarko

JorgeRpo

unread,
Oct 1, 2008, 12:05:22 AM10/1/08
to web2py Web Framework


yarko wrote:
> Annet-
>
> T2 is Massimo's work to put a higher level of abstraction on web2py.
> It's also been a way to work out how modules will work in web2py.
>
> You can find it (and the current document and some simple examples) at
> http://launchpad.net/t2
I went to launchpad but couldnt find any examples or docs. Can you
help me there?

voltron

unread,
Oct 1, 2008, 12:49:07 AM10/1/08
to web2py Web Framework

JorgeRpo

unread,
Oct 1, 2008, 10:16:51 AM10/1/08
to web2py Web Framework
thank you voltron

yarko

unread,
Oct 1, 2008, 1:06:30 PM10/1/08
to web2py Web Framework
You can find examples in the doc of t2 - which you find at
launchpad.net/t2 -->
code tab --> select main tab --> select source sub-tab --> select docs
folder; then get the pdf (icon on right of list to download).

The current version link (which may be out of date by the time someone
finds this post in the future) is:

http://bazaar.launchpad.net/%7Emdipierro/t2/main/download/mdipierro%40cs.depaul.edu-20080922181354-3rpjwvwvatl9cnim/t2.pdf-20080922043548-uj1qtzubnbxj3dm6-26/t2.pdf?file_id=docs-20080922043548-uj1qtzubnbxj3dm6-8

The docs listing (this one for revision 12):

http://bazaar.launchpad.net/~mdipierro/t2/main/files/12?file_id=docs-20080922043548-uj1qtzubnbxj3dm6-8

Regards,
Yarko

On Oct 1, 9:16 am, JorgeRpo <jorgeh...@gmail.com> wrote:
> thank you voltron
>
> voltron wrote:
> > here are some links to T2 stuff Jorge
>
> >http://groups.google.de/group/web2py/browse_thread/thread/87b976ed562...
> >http://groups.google.de/group/web2py/browse_thread/thread/db594e601ca...

Snoboardfreak

unread,
Oct 7, 2008, 7:31:25 AM10/7/08
to web2py Web Framework
hope its cool to add to this thread?

some good info here on different ways to make menu!

but i can't find way to make menu to have headers between links.

anyone know how to do this??

like:

**********
header eg cars
**********
link
link
link
**********
header eg bikes
**********
link
link
link

I'm making menu in controller like this:

app=request.application
response.menu=[
['home',request.function=='index','/%s/default/index'%app],

and view is like this

{{if response.menu:}}
<div class="mynav">
<ul>
{{for name,active,link in response.menu:}}
<li {{if active:}}id="nav-active"{{pass}}><a
href="{{=link}}">{{=name}}</a></li>
{{pass}}
</ul>
</div>
{{pass}}

but it doesnt have headers so looks too many links to user.

i can add headers by building static nav menu and putting it in every
view but too painful to update!

and i'm too new to try T2 and it looks too new too

and best i learn how to code my own magic first instead use T2s menu
magic!

again sorry for easy question for you pros!!

anyone done this before?
Reply all
Reply to author
Forward
0 new messages