using base layout templates in chameleon

2019-07-03 01:46发布

问题:

In the pyramid docs there is a nice tutorial on UX stuff here:

http://docs.pylonsproject.org/projects/pyramid_tutorials/en/latest/humans/creatingux/step07/index.html

One thing I noticed though is in the tutorial they are setting up and passing around the 'global layout' explicitly in the code (see below). I thought this was unusual and unnecessary because I've always just used the 'load' command as shown in the docs here:

http://chameleon.repoze.org/docs/latest/

Is this just a personal preference issue or are there real advantages to setting up and using the 'global layout' in this way?

Tutorial base view class:

class Layouts(object):
    @reify
    def global_template(self):
        renderer = get_renderer("templates/global_layout.pt")
        return renderer.implementation().macros['layout']

Tutorial template file:

<div metal:use-macro="view.global_template">
    <div metal:fill-slot="content">
        <p>Home page content goes here.</p>
    </div>
</div>

But in my template files I just use:

<div metal:use-macro="load: global_layout.pt">
    <div metal:fill-slot="content">
        <p>Home page content goes here.</p>
    </div>
</div>

回答1:

The indirect way (via view) gives you more flexibility. The benefits are not so obvious in a small project, but this approach surely pays off in a larger one. The "load:" is harcoding your main_template (in Zope/Plone-speak) to be here. With the view, it can come from anywhere and changed independently of your templates.