Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyramid framework and master templates / master page / partial views

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?

like image 817
ken Avatar asked Jun 29 '12 02:06

ken


1 Answers

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")
    ]
]
like image 131
Sean Vieira Avatar answered Oct 19 '22 13:10

Sean Vieira