Template inheritance from page.parent

67 views
Skip to first unread message

kenb...@gmail.com

unread,
Jul 11, 2012, 5:38:25 PM7/11/12
to mezzani...@googlegroups.com
I have a use-case where I want, for example, an "About" page with sub-pages. I want the sub-pages to use the same template as the "About" page. I can write templates for each sub-page, but that doesn't seem very DRY.

The solution that I have in mind is to create a template at /pages/about/richtextpage.html. Sub-pages such as "About/Team" would check first for /pages/about/team.html, then try  /pages/about/richtextpage.html, and finally fall back on /pages/richtextpage.html. Is this harebrained? Anyone have oter suggestions?

Brian Schott

unread,
Jul 11, 2012, 5:52:27 PM7/11/12
to mezzani...@googlegroups.com
Sounds like a great idea. Maybe you should look for {{ page.content_model }}.html since there are other page types?

"content_model": "richtextpage",
"content_model": "category",
"content_model": "form",

Gary Reynolds

unread,
Jul 11, 2012, 5:53:08 PM7/11/12
to mezzani...@googlegroups.com
I don't think it's hair-brained at all - I think it's the right way.

I often do something like:
subpage.html → extends page.html → extends base.html

You obviously need a good {% block %} structure to leverage what you want to achieve, but as far as a methodology goes I think it's a good one.

Gary

Gary Reynolds

unread,
Jul 11, 2012, 6:00:09 PM7/11/12
to mezzani...@googlegroups.com
Oh, after seeing Brian's response, I think I've answered a different question to the one you've asked.

You're talking about a not having a custom template *at all* for a sub-page and extending the template resolution order? I've already written something like this in one of my projects; I'll try and dig it out and put it in a gist.

Gary

kenb...@gmail.com

unread,
Jul 12, 2012, 2:17:59 PM7/12/12
to mezzani...@googlegroups.com
I believe Mezzanine already handles this case. Category pages will render an existing templates/pages/category.html, unless I am misunderstanding something. That would work nicely if I wanted to subclass Page, rather than using Page or doing field injection on the Page model. 

The relevant snippet of code is at mezzanine/pages/views.py:70-76. I am thinking about something inserted after line 71 like:

    templates.append(u"pages/%s/page.html" % template_name)
    if page.parent is not None:
        template_name = unicode(page.parent.slug)
        templates.append(u"pages/%s/page.html" % template_name)

That is obviously not adequate for deeply nested pages.

best,
ken

Stephen McDonald

unread,
Jul 15, 2012, 5:13:02 PM7/15/12
to mezzani...@googlegroups.com
If anyone's interested in this, we're currently working out a solution here:

--
Stephen McDonald
http://jupo.org
Reply all
Reply to author
Forward
0 new messages