Introducing PHP Site Mesh
We have been learning Grails at work and I couldn’t help but feel that the Site Mesh implementation is an awesome way of doing templating. I looked around for awhile to is if I could find an similar implementation in PHP but I only found one so far that seemed at all like what I was looking for. SiteMesh.PHP is a open project on SourceForge and I honestly just didn’t like the way it was implemented. It seemed very limited and general not as elegant as what I have seen in Grails. So I have set out to create my own implementation. So far so good. I should have something beta code to post soon.
For those unfamiliar with Site Mesh below is very simple example. The basic idea here is that all
of the pages you create are individually valid html pages. The contents of the body of a page are
inserted via a developer defined content tag within the a layout page. A page can have infinitely
nested layout pages. Additionally there are other helper tags that make templating your site very
straight forward. I think that this way of doing templating the especially helpful with PHP since
the use of incline <?php ?>
syntax is very unreadable. It creates a elegant abstraction between
business logic and view code.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:php="http://test.com">
<head>
<meta name="layout" content="Layout.html" />
<meta name="description" content="This is my page description." />
<title>Page Title</title>
</head>
<body>
<h1>Page</h1>
<p>Page content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:php="http://test.com">
<head>
<title><php:pageTitle /> - Layout Title</title>
</head>
<body>
<h1>Layout</h1>
<php:pageContent />
<p>Layout content</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:php="http://test.com">
<head>
<meta name="description" content="This is my page description." />
<title>Page Title - Layout Title</title>
</head>
<body>
<h1>Layout</h1>
<h2>Page</h2>
<p>Page content</p>
<p>Layout content</p>
</body>
</html>