Master pages and partial views in Sharpy

I recently introduced Sharpy – a view engine designed to allow developers and designers to work together.  Today I’m going to take a look at how master pages and partial views are used with this new view engine.

How master pages work in PHP

As far as I can tell PHP doesn’t really have the concept of master pages and therefore there was no functionality for me to copy from Smarty.  (I could be wrong – I’m no PHP expert – in fact, should I write php, PHP or Php?)  I had a look at some of the other MVC view engines to see how master pages were implemented and then decided on an approach.

Interestingly enough there is more than one way to specify a master page with the default MVC view engine – you can either specify the name of the master page in your view (which is the default way) or you can specify the master page in your controller by using an overload of the View method.  Some of the other view engines allow even more ways of specifying the master page – for example, the Spark view engine allows you to have an Application.spark file which will be used as a site-wide master page.

In this type of scenario I prefer to have only one way of doing something – I find it cuts down on the confusion – who really specifies the master page in the controllerUpdate: apparently quite a few people do.  In the latest release you can now override the master page in the controller.

How master pages work in Sharpy

I decided to follow the convention in the current MVC view engine and specify the master page as the first line in the view.

{master file='~/Views/Shared/Master.sharpy' title='Variable Modifiers'}

I initially tried to create a special type of function that takes the rest of the page as content but in the end I decided YAGNI and went with the easy solution.  I also had to decide how I want the content of the view to be rendered in the master page.  The default MVC view engine uses special placeholder tags which are obviously the most flexible but not necessarily the most straightforward.  In the end I decided to simply render the entire view into a variable called ‘content’ and allow the master page to render this variable.  (This is similar to the way the default rails view engine works)

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>{$title}</title>
</head>
<body>
    {$content}
</body>
</html>

Lastly I wanted the view to be able to set certain variables in the master page to be used during the evaluation.  In this example I have used this functionality to specify the title of the page.  You can set multiple variables in this way.

How partial views work in Sharpy

Smarty had the concept of partial views with the include function – I simply implemented a similar function.  The only real difference between this include function and the Html.RenderPartial method is that we don’t have the ability to change the model or view data.

{include file='~/Views/Home/Partial.sharpy'}

Happy coding.