Getting the Title of the current level0 node in Plone
This post was written 5 years ago.
Wed, 13 Dec 2006
This is not so much a "how-to", but more of a "is this how-to?", I post it here mainly to invite comment and expose my ignorance about the much cleaner method that probably exists.
OK, first my use case - in a skin I am building the title of the current level 0 node is always displayed as a section heading e.g. If you are viewing the page "eggs", a few levels down and the breadcrumb trail reads home -> foo -> bar -> eggs, "foo" is displayed as a banner heading, so wherever you are in the site you know what main section you are in.
so I have created a skin script called getLevelZeroTitle that has approximately this in it (with try/except statements, and a list of id's to ignore to handle errors):-
current_level0_path = '/'.join(context.getPhysicalPath()[:3])
section_id = context.getPhysicalPath()[2]
current_level0_title = context.portal_catalog(path=current_level0_path)[0].Title
return current_level0_title
Is this the way forward or is there something "built in" to handle this?
OK, first my use case - in a skin I am building the title of the current level 0 node is always displayed as a section heading e.g. If you are viewing the page "eggs", a few levels down and the breadcrumb trail reads home -> foo -> bar -> eggs, "foo" is displayed as a banner heading, so wherever you are in the site you know what main section you are in.
so I have created a skin script called getLevelZeroTitle that has approximately this in it (with try/except statements, and a list of id's to ignore to handle errors):-
current_level0_path = '/'.join(context.getPhysicalPath()[:3])
section_id = context.getPhysicalPath()[2]
current_level0_title = context.portal_catalog(path=current_level0_path)[0].Title
return current_level0_title
Is this the way forward or is there something "built in" to handle this?
This post was written 5 years ago, which in internet time is really, really old. This means that what is written above, and the links contained within, may now be obsolete, inaccurate or wildly out of context, so please bear that in mind :)
Comments
Funny, we had a very similar use-case recently.
We wrote a script that looked like this:
## Script (Python) "getSectionTitleForBanner"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=Figure out the appropriate title for the display banner
##
# Get the physical path to the context & the site portal
curr_path = context.getPhysicalPath()
portal = portal_path = context.portal_url.getPortalObject()
portal_path = portal.getPhysicalPath()
len_portalpath = len(portal_path)
from_root_path = curr_path[len_portalpath:]
if len(from_root_path) > 1:
if curr_path != portal_path:
from_root_path = from_root_path[:1]
from_root_path = "/".join(from_root_path)
display_section = portal.restrictedTraverse(from_root_path)
return display_section.Title()
--------
HTH. (I cut out a section that handled an exception where we wanted to not show level 0 folder sections).
Jon Stahl 2006-12-13 15:19:00
portal = context.portal_url.getPortalObject()
parent = context
if parent is portal: return portal.TItle()
while 1:
if parent.aq_parent is portal:
return parent.Title()
parent = parent.aq_parent
kapil 2006-12-15 00:26:01
I'm a newbie at python but was able to fathom your elegant script (once I saw the typo and realized where indenting was needed) and use it on the site I'm currently building.
Winn King 2006-12-25 02:46:05