金字塔框架和主模板/母版页/部分视图

时间:2012-06-29 02:20:06

标签: master-pages pyramid chameleon template-metal

我对.NET MVC很有经验,并且想要学习Python框架。我选择了金字塔。

.NET MVC具有母版页视图部分视图的概念。母版页看起来像:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
</head>
<body>
    <div>
        <asp:ContentPlaceHolder ID="MainContent" runat="server" />
    </div>
</body>
</html>

然后我可以创建一个视图,它将填充母版页中MainContent标识的空间。

通过金字塔维基教程here,我看到作者在他的每个模板中重复了大部分相同的内容 - 通常在母版页中定义的内容 - 并完全违反 DRY

金字塔中是否有母版页的概念?

1 个答案:

答案 0 :(得分:15)

就像MVC.NET Pyramid可以使用任意数量的模板语言一样 - 几乎所有这些语言都支持类似于母版页的概念。尽管如此,他们中没有人调用; - )

Chameleon可能是最远的地方 - 用于在母版页ContentPlaceholder中定义插槽的工具等等)在Chameleon中称为macros,并且由相当重的首字母缩略词引用{ {3}}

在Jinja2和Mako中,他们被称为blocks,而Breve称他们为slots

以下是每个主页的主页:

<强>变色龙

<!-- Caveat Emptor - I have never used Chameleon in anger -->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:tal="http://xml.zope.org/namespaces/tal"
  xmlns:metal="http://xml.zope.org/namespaces/metal"
  xmlns:i18n="http://xml.zope.org/namespaces/i18n">
<!-- We don't *need* all of this in Chameleon, but it's worth 
remembering that it adds it for us -->
<head>
<title metal:define-macro="title"><span metal:define-slot="title"></span></title>
</head>
<body metal:define-macro="content">
<div metal:define-slot="content"></div>
</body>
</html>

<强>的Jinja2

<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>

<强>真子

<!DOCTYPE html>
<html>
<head>
<title><%block name="title" /></title>
</head>
<body>
<%block name="content" />
</body>
</html>

<强>短音

html [
    head [
        title [ slot("title") ]
    ]
    body [
       slot("content")
    ]
]
相关问题