Phil's blog

Apr 2014
Mo Tu We Th Fr Sa Su
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

Cheetah templates overriding

Cheetah is a very good templating language, especially because it's the one (correct me if i'm wrong) to apply the Object-Oriented model to Documents. It can seem a bit weird at the beginning but when you get used to it, you can't live without it :-)

So, i was looking for a good way to overload template parts without pain, here is how (couldn't find the trick on Cheetah doc):

  1. Assume you have a template Package/Page.tmpl like that:

    #def footer
      This is my foot !
    #end def
  2. Create a Page_custom.tmpl template:

    #extends Package.Page
    #def footer
      $Page.footer($self) Where's my mind ? (dixit the Pixies)
    #end def
  3. You need to put a self variable in the template's searchList which is the Cheetah template instance itself :-)

I don't think that trick will work if you don't compile your templates to Python code.

by Philippe Normand on Tue Mar 22 09:01:25 2005 (Viewed: 25802 / 2 comments )
  |   RSS  |   RSS2  |   Atom  |   Source  |   Edit

#.   deelan on Fri Aug 12 13:27:27 2005

"I don't think that trick will work if you don't compile your templates to Python code."

Correct, actually you have to precompile the base class.

In Subway[1] we managed to automatically compile the whole template hierarchy upon request. Builiding web site using Cheetah and his #extends directive is great. As you have written you can write some default markup into parent template #blockS and then override only when you really need to. No more ugly includes :)

For example, base site.html:

and one view which overrides both "content" and "nav" #blocks:

-- deelan


#.   phil on Fri Aug 12 14:16:57 2005

Good point Deelan ;)

Actually the same trick is used by the framework behind this blog, but with #defS instead of #blockS

See the basic template:

And one custom template (the #extends is added a posteriori):

Comments not allowed anymore on this post