generate menu with category headers

27 views
Skip to first unread message

Snoboardfreak

unread,
Oct 7, 2008, 3:44:46 PM10/7/08
to web2py Web Framework
hi all
(me again sorry)

anyone generated vertical menu with category headers between links?

i was looking at annets post on how to generate menus
http://groups.google.com/group/web2py/browse_thread/thread/9dc3b60c529a04e9

and got my own menu going using a controller with css and view.
but i cant find way to make menu to have headers between links

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="mymenu">
<ul>
{{for name,active,link in response.menu:}}
<li {{if active:}}id="mymenu-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 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!!
someone must have done already bcoz very popular menu??

thx

Snoboardfreak

unread,
Oct 7, 2008, 5:29:13 PM10/7/08
to web2py Web Framework
also wonderng how to generate 2 different menus in same page

from different controllers?
one horiztonal and other vertical
each differnt links

when i try putting menus in different controllers i get same menu

for horizontal menu i put generated menu in default.py and
mylayout.html
for vertical menu i put generated menu in myview.py and myview.html
(extends mylayout.html)

but myview.html shows horiztonal and vertical menus with SAME links
from myview.py

horizotnal should be from default.py and verticl from myview.py

please help and if u know how to add headers to vertical very thankful

these are stupid question but i cant find answer anywhere
and i need these simple page elements
updating static menu kills my fingers and brain!

thank u pros

snoboardfreak

Snoboardfreak

unread,
Oct 7, 2008, 10:07:34 PM10/7/08
to web2py Web Framework
yo web2pyfreakz!

ANYONE NEED COFFEE?? :-)

if you've drunk from da brew, any thoughtz on how to

- add headers to controller generated menus?

- render 2 menus (horizontal and vertical) in same view from different
controllers?

(so far i can get a vertical menu *without* headers generated)

these problems should be eazy for web2py pros, no?

must be bcoz i dont see anyone else asking how to make such common
simple things.

if i missed something in the manual or alter ego, please tell me i'm
an idiot!

or please help with some ideas. :(

(wondering if anyone else here uses common menus like me: tabs,
vertical css menu with headers?? =)

mdipierro

unread,
Oct 7, 2008, 10:50:59 PM10/7/08
to web2py Web Framework
Sorry. Busy watching the debate.

I am not sure I understand the question. If I do this is not a web2py
question. This is an HTML question. In the layout.html there is a
piece of code that generates a menu (in HTML) based on the variable
response.menu. If you use T2 the layout.html uses JS to make a more
flashy menu.

These are just examples. web2py does not build menus. You do. You
should find a JS library or CSS/HTML template that makes the menu that
you want and customize them.

Again, perhaps I misunderstood what you need. Feel free to ask again.

Massimo

Snoboardfreak

unread,
Oct 7, 2008, 11:35:48 PM10/7/08
to web2py Web Framework
politics? hehe freaky!
(go O!)

hmm...i try to explain better.

i have 2 problems with making menus in web2py
i can use css and js files to make nice menu
but i need generated menus that use css
i followed link in first post and made a cool vertical menu generated
from controller.
but it has no headers between links so looks messy.

my first problem
i have no idea how to code headers into generated menu using python.
tried and tried but failed last couple of nights.
or should i be trying to make menu from controller without
response.menu?
i dont need any fancy effex just need a menu like i get with
response.menu in controller but with headers between links.


my other problem
other problem is including 2 menus a horiztonal and vertical in same
view with each generated from different controllers
-horozontal comes from default.py
-vertical comes from myview.py
-both menus should be in myview.html.
-myview.html extends mylayout.html which also includes horiztonal menu
from default.py

when i try to generate 2 menus a vertical and horozintal on same page
i get same links in both.

sorry for stupid questions massimo
hoping web2py makes generating menus easy

who 'won' debate?

snoboardfreak

mdipierro

unread,
Oct 8, 2008, 1:07:27 AM10/8/08
to web2py Web Framework
Here is my suggestion:

submenu1=[
['item1',False,URL(r=request,f='item1')],
['item2',False,URL(r=request,f='item2')],
]

submenu2=[
['item3',False,URL(r=request,f='item3')],
['item4',False,URL(r=request,f='item4')],
]

response.menu=[
['header1',false,'#',submeny1],
['header2',false,'#',submeny2],
]

and in view you can do something like this

{{for head in response.menu:}}
<b>{{=A(head[0],_href=head[2])}}</b><ul>
{{for item in head[4]:}}<li>{{=A(item[0],_href=item[2])}}</
li>{{pass}}
</ul>
{{pass}}

You can have another menu by using

response.my_other_menu and in view

{{for head in response.my_other_menu:}}
<b>{{=A(head[0],_href=head[2])}}</b><ul>
{{for item in head[4]:}}<li>{{=A(item[0],_href=item[2])}}</
li>{{pass}}
</ul>
{{pass}}

You can then give classes to the h2, ul, li tags to customize using
css.

T2 defines: T2.menu(response.menu) which generates a menu compatible
with
http://users.tpg.com.au/j_birch/plugins/superfish/

Massimo

Snoboardfreak

unread,
Oct 8, 2008, 6:05:34 PM10/8/08
to web2py Web Framework
thanks sooo much massimo!!!

i'm trying your code now and getting this error

ValueError: too many values to unpack

any ideas why?

mdipierro

unread,
Oct 8, 2008, 6:56:17 PM10/8/08
to web2py Web Framework
Sorry:

{{for head in response.my_other_menu:}}
<b>{{=A(head[0],_href=head[2])}}</b><ul>
{{for item in head[3]:}}<li>{{=A(item[0],_href=item[2])}}</
li>{{pass}}
</ul>
{{pass}}

Snoboardfreak

unread,
Oct 8, 2008, 11:15:54 PM10/8/08
to web2py Web Framework
damn i'm still getting the same error

sorry

mdipierro

unread,
Oct 9, 2008, 1:10:02 AM10/9/08
to web2py Web Framework
I tried this and it worked for me:

>>> submenu1=[
... ['item1',False,URL(r=request,f='item1')],
... ['item2',False,URL(r=request,f='item2')],
... ]
>>>
>>> submenu2=[
... ['item3',False,URL(r=request,f='item3')],
... ['item4',False,URL(r=request,f='item4')],
... ]
>>> response.menu=[
... ['header1',False,'#',submenu1],
... ['header2',False,'#',submenu2],
... ]
>>> for head in response.menu:
... print A(head[0],_href=head[2])
... for item in head[3]:
... print '-',A(item[0],_href=item[2])
...
<a href="#">header1</a>
- <a href="/test/default/item1">item1</a>
- <a href="/test/default/item2">item2</a>
<a href="#">header2</a>
- <a href="/test/default/item3">item3</a>
- <a href="/test/default/item4">item4</a>

phyo.ar...@gmail.com

unread,
Oct 9, 2008, 9:53:08 AM10/9/08
to web...@googlegroups.com
Sno

You can try downloading a HTML/CSS template with good menus -(many
there at www.openwebdesign.com) , and make web2py use it. you can
learn how to make web2py use it by how layout.html is made.

try to adapt the template with web2py's layout.html and you will get
custom menus that you like.

Snoboardfreak

unread,
Oct 9, 2008, 5:47:37 PM10/9/08
to web2py Web Framework
thanks Massimo and phyo!

no idea why but after trying Massimos latest code i get same error

ValueError: too many values to unpack

this is whats in the ticket

if response.menu:
response.write('\n <ul>\n ',escape=False)
for name,active,link in response.menu:
response.write('\n <li ',escape=False)
if active:
response.write('id="nav-active"',escape=False)
pass
response.write('><a href="',escape=False)
response.write(link)
response.write('">',escape=False)
response.write(name)
response.write('</a></li>\n ',escape=False)
pass
response.write('\n </ul>\n\n ',escape=False)
pass

may be this stupid question of mine will solve the error

is this what goes in the view?

{{for head in response.menu:
print A(head[0],_href=head[2])}}
{{for item in head[3]:
print '-',A(item[0],_href=item[2])}}

or am i putting {{}} in wrong places?

thanks again mates this is really helping me
and hopefully a few other newbies also!!

yeah I been getting some nice layouts and menus in web2py from some
cool css sites.
applied them to the first generated menu i made. hope to do same with
this one with headers.
checked openwebdesign before and its a sweet place!

Snoboardfreak

unread,
Oct 10, 2008, 7:14:14 PM10/10/08
to web2py Web Framework
I went back to Massimos original code...

and now I get this error:

TypeError: 'NoneType' object is not iterable

ticket:

for head in response.menu:
response.write('\n <b>',escape=False)
response.write(A(head[0],_href=head[2]))
response.write('</b>\n <ul>
\n ',escape=False)
for item in head[4]:
response.write('\n <li>',escape=False)
response.write(A(item[0],_href=item[2]))
response.write('</li>\n ',escape=False)
pass
response.write('\n </ul>\n
',escape=False)
pass

what do ya think?

Snoboardfreak

unread,
Oct 12, 2008, 5:11:44 PM10/12/08
to web2py Web Framework
hey pros!

anyone tried above code and got same error?

or am i doing something (very) stupid?

any help great!!

tx

mdipierro

unread,
Oct 12, 2008, 7:33:53 PM10/12/08
to web2py Web Framework
I tried this view with no problems:

---- begin file index.html ----
{{extend 'layout.html'}}
{{submenu1=[
['item1',False,URL(r=request,f='item1')],
['item2',False,URL(r=request,f='item2')],
]
submenu2=[
['item3',False,URL(r=request,f='item3')],
['item4',False,URL(r=request,f='item4')],
]
response.my_other_menu=[
['header1',False,'#',submenu1],
['header2',False,'#',submenu2],
]
}}
{{for head in response.my_other_menu:}}
<b>{{=A(head[0],_href=head[2])}}</b><ul>
{{for item in head[3]:}}<li>{{=A(item[0],_href=item[2])}}</
li>{{pass}}
</ul>
{{pass}}
---- end file ---


Massimo
Reply all
Reply to author
Forward
0 new messages