开发者

Good way to pass variables for common elements to Mako templates?

I'm using Mako's inheritance features to factor out common page elements, like a header and footer, into a "base.mako" template. Page-specific controllers use their own templates, which inherit base.mako. 开发者_运维百科

base.mako needs a set of variables -- for example, the name of the logged-on user goes in the header for all pages. However, it's the individual page controller that selects and renders the template, and is therefore responsible for passing variables to it.

I'd rather all of those page controllers not have to know what the base template needs in its context. What's a good way to establish the context for the base template in a situation like this?

Thanks in advance!


You have two OO design choices for your page controllers.

Common features can be handled two ways.

  • Inheritance. All page controllers are subclasses of a common class that provides the common attributes.

  • Delegation. All page controllers are part of a pipeline where some common process (either before or after the page controller) folds in the additional information.

You have to pick one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜