How to get page title in html title tag?

152 views
Skip to first unread message

Claudinei Pereira

unread,
Jul 1, 2011, 4:10:26 AM7/1/11
to blogofil...@googlegroups.com
Hi,

I was able to set the html title tag for every post of my blog (Thanks
Morgan Goose for this!), but i'm
still not getting an unique title tag for pages that are not posts
(about.html.mako, for example). Any hint how to get it? I guess it's
needed to set the title on the page, for example

# about.html.mako
<%
page_title = "About"
%>
[other page stuff]

But how to get this info on _templates/head.mako??? I tried Morgan's
approach and it's not working:

# head.mako
[stuff]
% if hasattr(self, "page_title"):
<title>${self.page_title}</title>
[moar stuff]

Thanks in advance!

--
www.claudineipereira.com

Frankie Bagnardi

unread,
Nov 14, 2012, 3:54:24 AM11/14/12
to blogofil...@googlegroups.com, con...@claudineipereira.com
Hey,

I was able to set the html title tag for every post of my blog (Thanks
Morgan Goose for this!)


Where can I find out how to do this?  I can't seem to find anything about it on the mailing list...

Thanks,
Frankie

EJelome

unread,
Nov 14, 2012, 5:42:58 AM11/14/12
to blogofil...@googlegroups.com, con...@claudineipereira.com
Hi Claudinei,

There's seems to be no trivial way to get the a page's title unlike posts, however, there's a way where you can get the title, here's how I did it.

Using this:
bf.template_context.render_path

It will render the path of the current page you're viewing, something like:
Home
./index.html

Blog
/blog/index.html

About
about/index.html

Contact
contact/index.html

Sitemap
contact/index.html

Doing this is easy, first go to your root directory and create a new directory (e.g., about). Then in that directory, create an index.html.mako file which will represent that directory.

Now when you go to that directory, you'll see that the value of bf.template_context.render_path will be about/index.html.

The way I generated the name was by doing:
path = bf.template_context.render_path
page_title = path.split('/')[0].capitalize()


In case you don't know some of the functions used here:
.split('/'): 'splits' the value that's between '/' and creates a list
[0]: get the first index of that list (index starts at 0 not 1)
.capitalize(): will make all first letter of every word to uppercase

Kind regards,
EJelome

Doug Latornell

unread,
Nov 14, 2012, 10:55:43 AM11/14/12
to blogofil...@googlegroups.com

On Wed, Nov 14, 2012 at 2:42 AM, EJelome <eje...@gmail.com> wrote:
...


The way I generated the name was by doing:
path = bf.template_context.render_path
page_title = path.split('/')[0].capitalize()


A small suggestion to make this more Pythonic, and cross-platform:

  import os
  ...
  page_title = os.path.dirname(path).capitalize()

Péter Zsoldos

unread,
Nov 14, 2012, 3:19:07 PM11/14/12
to blogofil...@googlegroups.com
since this issue seems to be popping up (e.g.: https://github.com/EnigmaCurry/blogofile_blog/issues/18), maybe having  a template variable normalized_render_path (linuxified) or, render_path_parts (a list of folder names + the file name as the last) OR a helper method bf.get_render_path_part(index) could hide any platform issues away from the templates

What do you think?

Péter Zsoldos

unread,
Nov 14, 2012, 3:21:23 PM11/14/12
to blogofil...@googlegroups.com
I might be hijacking the thread, but I've run into this issue in a more general way (disclaimer: I'm mostly a back end application programmer, so I might be trying to do the wrong thing here on the frontend). E.g.: on certain pages, I would like to include some extra javascript or css files in the page; or customize the <title> *while retaining the site wide title settings *, e.g.: About us | [site configured title] - so that if I change the general wording of the title in the config/head.mako I don't have to remember to grep across all files to ensure the customized pages are consistent. 

Also, I try to minimize the amount of python code I need to write in my templates, since I've found that rather helps when working with non-developers (e.g.: designers). A solution similar to django's template inheritance https://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance would be nice - though we might not need something so advanced/complicated

And of course, this might be possible in mako too - let me know if so (or give me some pointers where to start searching).

Just 2c (until I will find enough free time to dive into the code),

Peter

On Wed, Nov 14, 2012 at 11:42 AM, EJelome <eje...@gmail.com> wrote:
There's seems to be no trivial way to get the a page's title unlike posts, however, there's a way where you can get the title, here's how I did it.
[...] 

EJelome

unread,
Nov 18, 2012, 7:58:46 PM11/18/12
to blogofil...@googlegroups.com, d...@douglatornell.ca
Hi Doug, thanks for the alternative script --- it's much better. ^^, 

Doug Latornell

unread,
Nov 22, 2012, 4:59:53 PM11/22/12
to blogofil...@googlegroups.com, Péter Zsoldos
On Wed, Nov 14, 2012 at 12:21 PM, Péter Zsoldos <peter....@gmail.com> wrote:
I might be hijacking the thread, but I've run into this issue in a more general way (disclaimer: I'm mostly a back end application programmer, so I might be trying to do the wrong thing here on the frontend). E.g.: on certain pages, I would like to include some extra javascript or css files in the page; or customize the <title> *while retaining the site wide title settings *, e.g.: About us | [site configured title] - so that if I change the general wording of the title in the config/head.mako I don't have to remember to grep across all files to ensure the customized pages are consistent. 

Also, I try to minimize the amount of python code I need to write in my templates, since I've found that rather helps when working with non-developers (e.g.: designers). A solution similar to django's template inheritance https://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance would be nice - though we might not need something so advanced/complicated

And of course, this might be possible in mako too - let me know if so (or give me some pointers where to start searching).

There's a couple of ways in Mako to to this sort of thing, the %block construct, and %def.

I typically have a site.mako template that has all the site-wide HTML boilerplate, links and scripts needed on every page (e.g. jQuery, site CSS, etc.). In that template I put a block like:

<title><%block name="title">Site Title</%block></title>

Other templates inherit from site.mako (maybe through multiple layers of inheritance), but they can override the default site title with:

<%block name="title">Page Title</%block>

%def is more flexible because it can accept args, and it looks like that's what your example above is describing. See the Mako docs. In the context of a Pyramid or Django web app I'd just pass variables into the template. In the Blogofile context you'd probably have to write a filter to get the variables you wanted to use in the correct place for your %defs to use. I'm just suggesting that off the top of my head though - there might be an easier way.


Just 2c (until I will find enough free time to dive into the code),

Peter

On Wed, Nov 14, 2012 at 11:42 AM, EJelome <eje...@gmail.com> wrote:
There's seems to be no trivial way to get the a page's title unlike posts, however, there's a way where you can get the title, here's how I did it.
[...] 
path = bf.template_context.render_path
page_title = path.split('/')[0].capitalize()

--
You received this message because you are subscribed to the Google Groups "blogofile-discuss" group.
To post to this group, send email to blogofil...@googlegroups.com.
To unsubscribe from this group, send email to blogofile-disc...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/blogofile-discuss?hl=en.

Kelly Wilkerson

unread,
Jun 19, 2014, 3:29:56 PM6/19/14
to blogofil...@googlegroups.com, peter....@gmail.com, d...@douglatornell.ca


There's a couple of ways in Mako to to this sort of thing, the %block construct, and %def.

I typically have a site.mako template that has all the site-wide HTML boilerplate, links and scripts needed on every page (e.g. jQuery, site CSS, etc.). In that template I put a block like:

<title><%block name="title">Site Title</%block></title>

Other templates inherit from site.mako (maybe through multiple layers of inheritance), but they can override the default site title with:

<%block name="title">Page Title</%block>


I feel like I've lost my mind somewhat, because this is precisely what I tried, but it doesn't change the titles for any of the blog-controller generated pages. Are you able to change archive/index.html in this way, for example?  
Reply all
Reply to author
Forward
0 new messages