将通用元素的变量传递给 Mako 模板的好方法?

发布于 2024-08-12 15:55:33 字数 247 浏览 3 评论 0原文

我使用 Mako 的继承功能将常见页面元素(例如页眉和页脚)分解为“base.mako”模板。页面特定的控制器使用自己的模板,该模板继承base.mako。

base.mako 需要一组变量——例如,登录用户的名称出现在所有页面的标题中。但是,选择和呈现模板的是单独的页面控制器,因此负责将变量传递给它。

我宁愿所有这些页面控制器都不必知道基本模板在其上下文中需要什么。在这种情况下,建立基本模板上下文的好方法是什么?

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲琵琶半遮面シ 2024-08-19 15:55:33

您的页面控制器有两种 OO 设计选择。

常见的特征可以通过两种方式处理。

  • 继承。所有页面控制器都是提供公共属性的公共类的子类。

  • 代表团。所有页面控制器都是管道的一部分,其中一些公共进程(在页面控制器之前或之后)折叠在附加信息中。

你必须选择一个。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文