The Smarty Resource
Along with the standard resource class, Tonic has another resource class that extends the standard resource class but adds integration with the Smarty templating engine.
The class loads a copy of the Smarty object allowing data to be pushed into Smarty for use within the response representation. It is mostly useful for creating representation resources that simply read a parent resources data and displays it in a particular format.
An example Smarty resource
Using the Smarty resource is easy, simply add SmartyResource as the class in the representation metadata to cause the request object to load the resource as a Smarty resource:
mimetype: text/html
class: SmartyResource
title: Hello World
<html>
<body>
<h1>{$this->title|escape}</h1>
</body>
</html>
The metadata of the resource is directly accessable in Smarty as members of the
$this object and other data can be made available by extending the SmartyResource
and adding your own data access code.
Features
The Smarty resource provides the following Smarty related features:
Automatically assigned Smarty variables
The class assigns some Smarty variables for you:
- The resource itself is assigned to the Smarty variable
$this, this allows your Smarty template to access all of the resources properties. - If the representation we're viewing is just a representation and not the actual
resource, then you'll find the resource in the
$resourceSmarty variable. If not, then it will contain the actual resource, so this variable is always bound to the loaded resource. - The request object for the incoming request is bound to the Smarty
variable
$request. - Finally, the persistance adapter that the resource is using is bound to the Smarty
variable
$adapterjust incase it's useful to you.
The process modifier
A single Smarty variable modifier is also defined. The process modifier
can be used to process the contents of the variable for additional Smarty markup. This
is useful if you are including resource properties within your response representation
but want to include dynamic Smarty variables within those properties.
mimetype: text/plain
class: SmartyResource
message: My hobby: Insisting that {$this->thing} are photoshopped.
thing: real-life objects
{$this->message|process}
Back to documentation home