Adding a generic HTML shell to resource representations
We often want to include a standard header and footer around a HTML page so that we don't need to include them in every pages representation. This is a common Web app situation, so how do we accomplish this the Tonic way?
The main job of our resource class is to generate a response to an incoming request. Part of that response is the output representation, so we can write another resource to use as our representation and using Smarty stuff our page resources data into the representation.
mimetype: text/html
class: SmartyResource
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>{$resource->title|escape}</title>
</head>
<body>
<h1>My Site Title</h1>
{$resource->content|process}
<p class="footer">Last modified {$resource->modified|date_format}</p>
</body>
</html>
resources/shell.html
The line {$resource->content|process} includes our
resources content, the process modifier makes the SmartyResource
class process the contents of the string for Smarty tags before outputting it, so any
Smarty markup in our representation will be processed.
Finally we need to create our resource itself containing the content of our page and using the shell representation:
title: Hello World
representation: /shell.html
<h2>Hello World</h2>
<p>Hello, it's great to be here in {$smarty.now|date_format}!</p>
resources/hello.html
Now visiting /hello.html will load the our resource, which will
load the shell representation, pass it to Smarty, which will stuff in our resources
content and send the response back to the browser. Magic, and no PHP required.