template inheritance

69 views
Skip to first unread message

drebbin

unread,
Mar 4, 2011, 6:21:18 AM3/4/11
to pylons-...@googlegroups.com
Hi,
with ZPT templates is it possible for a view template to inherit from a layout template? Or have I to go with mako instead?
Somewhere the internet had an example, but I cannot find it anymore and am not quite sure if it was for zpt or mako.
KR, drebbin

Daniel Nouri

unread,
Mar 4, 2011, 7:03:01 AM3/4/11
to pylons-...@googlegroups.com, drebbin
On Fri, Mar 4, 2011 at 12:21 PM, drebbin <dirk...@gmx.net> wrote:
> with ZPT templates is it possible for a view template to inherit from a
> layout template?

Yes. Take a look at http://chameleon.repoze.org/docs/latest/metal.html


--
Daniel Nouri
http://danielnouri.org

drebbin

unread,
Mar 4, 2011, 8:03:59 AM3/4/11
to pylons-...@googlegroups.com, drebbin
Hmm. This bears several *ouch*:

1. The doc states, to reference a macro in a template that is defined in another template, the view has to put the referenced template into the available variables. Then, the view is involved in the page layout, which I dislike. The template should specify its layout and the view should only provide the data.

2. Besides looking cumbersome with that (define|use) (macro|slot) stuff, what I had in mind would render illegal XML. The inherited layout template should contain the complete page structure, including the opening HTML tag and the doctype. Afaiu all content I want to reference elsewhere has to be enclosed in a define-macro block. So the needed doctype and HTML tag had to be inside the define-macro block as well. But I cannot use the define-macro attribute, because its namespace is defined inside that block.

In mako this looks intuitive and easy:

templates/layouts/default.mak:
<!DOCTYPE html>
<html>
<body>${next.body()}</body>
</html>

templates/index.mak
<%inherit file="layouts/default.mak" />
<p>blah</p>

Or am I missing sth?

KR, drebbin

Daniel Nouri

unread,
Mar 4, 2011, 8:48:13 AM3/4/11
to pylons-...@googlegroups.com, drebbin
On Fri, Mar 4, 2011 at 2:03 PM, drebbin <dirk...@gmx.net> wrote:
> Hmm. This bears several *ouch*:
> 1. The doc states, to reference a macro in a template that is defined in
> another template, the view has to put the referenced template into the
> available variables. Then, the view is involved in the page layout, which I
> dislike. The template should specify its layout and the view should only
> provide the data.

What I do is I wrap template variables into a TemplateAPI object and
send it along to every template. You can also use the IBeforeRender
event to provide globals to the template, such as your master
template.

> 2. Besides looking cumbersome with that (define|use) (macro|slot) stuff,
> what I had in mind would render illegal XML. The inherited layout template
> should contain the complete page structure, including the opening HTML tag
> and the doctype. Afaiu all content I want to reference elsewhere has to be
> enclosed in a define-macro block. So the needed doctype and HTML tag had to
> be inside the define-macro block as well. But I cannot use the define-macro
> attribute, because its namespace is defined inside that block.

Look at the example in the docs that you linked to. When you
use-macro, you'll get the whole layout page and override only those
blocks that you're interested in with fill-slot. Anything in the
inheriting template that's not inside fill-slot will be ignored.

> In mako this looks intuitive and easy:
> templates/layouts/default.mak:
> <!DOCTYPE html>
> <html>
> <body>${next.body()}</body>
> </html>
> templates/index.mak
> <%inherit file="layouts/default.mak" />
> <p>blah</p>
> Or am I missing sth?

This looks indeed more intuitive than metal for what you're looking for.

drebbin

unread,
Mar 4, 2011, 9:19:35 AM3/4/11
to pylons-...@googlegroups.com, drebbin
Sorry, It seems to be the other way round:

layouts/default.pt:
<span metal:define-macro="content">
 <div id="content">
  <span metal:define-slot="content">content</span>
 </div>
</span>
<p>misc in default</p>
</html>

       xmlns:tal="http://xml.zope.org/namespaces/tal"
       xmlns:metal="http://xml.zope.org/namespaces/metal">
   <span metal:use-macro="default.macros['content']">
     <span metal:fill-slot="content">Hello from ZPT</span>
   </span>
   <p>misc in index</p>
 </html>

The rendered page shows "Hello from ZPT" and "misc in index" ("misc in default" is ignored). Which reflects exactly what the code states.

So one would have to invert the calling of the templates. Instead declaring "index.pt" as the view's renderer, I'd declare "layouts/default.pt". And the view provides template "index.pt" in the data, e.g. as key "content".
Can that be?


Daniel Nouri

unread,
Mar 4, 2011, 10:53:16 AM3/4/11
to pylons-...@googlegroups.com, drebbin

In your layout, use define-macro on the HTML element:

define-macro="content">
<div id="content">
<span metal:define-slot="content">content</span>
</div>

<p>misc in default</p>
</html>

Same with use-macro in your index.pt:

metal:use-macro="default.macros['content']">
<span metal:fill-slot="content">Hello from ZPT</span>

<p>misc in index</p>
</html>

The resulting HTML will be all of layout.pt, with only the "content"
slot replaced.

HTH,
Daniel

drebbin

unread,
Mar 4, 2011, 4:20:09 PM3/4/11
to pylons-...@googlegroups.com, drebbin
Oh, thanks. That works as you said.

Some peculiarities catch my attention:
1. At first it seems, we can use an attribute in the same tag that pulls the namespace where that attribute is defined. I do not know if that is valid XML, am no expert here.
2. Chameleon ZPT seem not to bother about that, because I can remove the xmlns-declarations altogether.
3. Chameleon ZPT also does not complain if I use a fantasy tag.

Unfortunately, still I find no way to put a valid doctype declaration into the layout template.


KR, drebbin

Daniel Nouri

unread,
Mar 5, 2011, 5:02:05 AM3/5/11
to pylons-...@googlegroups.com, drebbin
On Fri, Mar 4, 2011 at 10:20 PM, drebbin <dirk...@gmx.net> wrote:
> Oh, thanks. That works as you said.
> Some peculiarities catch my attention:
> 1. At first it seems, we can use an attribute in the same tag that pulls the
> namespace where that attribute is defined. I do not know if that is valid
> XML, am no expert here.

Not sure. nxml mode complains anyway.

> 2. Chameleon ZPT seem not to bother about that, because I can remove the
> xmlns-declarations altogether.
> 3. Chameleon ZPT also does not complain if I use a fantasy tag.
> Unfortunately, still I find no way to put a valid doctype declaration into
> the layout template.
> Code example: http://paste.pocoo.org/show/348195/

The doctype goes to the top of your index.pt


--
http://danielnouri.org
+49 151 28128966

drebbin

unread,
Mar 9, 2011, 2:54:37 AM3/9/11
to pylons-...@googlegroups.com, drebbin

The doctype goes to the top of your index.pt 

Yes. That seems to be the only way. In a perfect world, one could write the doctype that logically belongs to the outer template in the outer template, not in the inner template. But which world is perfect... *sigh*

Btw, interesting project you have, Kotti. Hope you don't mind if I take a closer look. Just started to learn pyramid and am glad to find working code examples. That inheriting ACL thing is especially interesting.

KR, drebbin
Reply all
Reply to author
Forward
0 new messages