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

Dependency Injection with PHP

Found this article while looking for an open source Dependency Injection engine for PHP.  The grammar needs a little work but otherwise it is a really good summary of what Dependency Injection is and why programmers should use it.

In ColdFusion we use ColdSpring which is based on the Spring Framework for Java.  It actually does a lot more than just DI but I am not concerned about that.  What I really want is something that can handle the same kind of DI situations that ColdSpring does for PHP.  I am currently working on a WordPress plugin for a freelance client which has turned out to be the most heavily object oriented PHP project I have done so far.  I have been trying to translate a lot the best practices I have learned while working with ColdFusion over to PHP and came across a need for DI.  What I ended up doing is creating a Factory that handles all of my object creation just like what is mentioned in the article by Ryan on PotStuck.  I pass an array with all of my classes, their paths, and their dependencies, like so:

$classes = array (
	'EventBean' => array (
		'path' => 'model/Event.php',
		'dependency' => array (
			'Venue' => 'VenueBean'
		)
	),
	'VenueBean' => array (
		'path' => 'model/Venue.php'
	)
);

So far it is working pretty well but I am thinking about making a more substantial DI framework that I can use on many projects to come.

Retweet