Pyramid framework and master templates / master page / partial views
Asked Answered
R

1

9

I'm experienced with .NET MVC and wanting to learn a Python framework. I chose Pyramid.

.NET MVC has the concept of a master page, views and partial views. A master page would look something like:

<%@ 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>

I can then create a view that would fill in the space identified by MainContent in the master page.

Going through the Pyramid wiki tutorial here, I see the author has repeated much of the same content in each of his templates--content that would normally be defined in a master page--and totally violated DRY.

Is there a concept of a master page in Pyramid?

Rollie answered 29/6, 2012 at 2:20 Comment(2)
In addition to @SeanViera's answer, have a look at this answer: https://mcmap.net/q/983138/-how-to-use-template-inheritance-with-chameleon - for an example of how you can pass a master template to a view with Chameleon.Resonate
There are two major code-reuse methods implemented by most modern template engines: * one template can include other templates or fragments of templates * one template can inherit from another template in order to change or extend parent templateRedroot
P
16

Just like MVC.NET Pyramid can use any number of templating languages - and almost all of them support concepts similar to master pages. None of them call them that though ;-)

Chameleon is probably the most far out there - the tools that you use to define slots in master pages ContentPlaceholder, etc.) are called macros in Chameleon and referred to by the rather heavy acronym METAL (Macro Expansion Template Attribute Language).

In Jinja2 and Mako they are called blocks and Breve calls them slots.

Here is what a master page might look like in each of them:

Chameleon:

<!-- 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>

Mako:

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

Breve:

html [
    head [
        title [ slot("title") ]
    ]
    body [
       slot("content")
    ]
]
Philippe answered 29/6, 2012 at 4:0 Comment(2)
Thanks. I had read about macros, but for some reason I thought they were used for something else. I'll look into them further.Rollie
+amillion for including examples for loads of templating languages in there. :-)Hayleyhayloft

© 2022 - 2024 — McMap. All rights reserved.