Smutty
MVC Framework
SmuttyViews
Smutty uses Smarty for it's templating system, so everything you want to know about using views in Smutty should be covered by the actual Smarty manual.
The only thing Smutty has to do with it is how you assign variables and display a template. To assign variables to a template just call the controllers set() method.
function showAction( $data, &$session ) {
$this->set( 'myvar', $data->string('myvar') );
// more code...
}
Then you show templates using the view() method. If you don't specify a template to use (the path given should be from the root of the views folder), then Smutty will assume the template to be named after the current action, with a .tpl extension.
class PostController extends Smutty_Controller {
function showAction( $data, &$session) {
$this->set( 'myvar', 2 );
$this->view(); // will assume you meant 'post/show.tpl'
}
}
Smarty plugins
When you want to add your own Smarty plugins, you will need to put these in the plugins directory inside the application directory. Smutty also provides a number of very useful functions and modifiers for use in your applications.
Smutty already comes with a number of plugins designed to make writing your templates easier. These are described below.
link and url
The link and url function make creating links are urls in your application a piece of cake! They adapt properly to any custom routes you have defined, and just take in the names of the parameters in the url, in any order.
<p>
{link url={ controller="post" action="show" id="1" } text="My Link"}
</p>
<form method="post" action="{url action="save" id=$post->id}">
</form>
The above example shows both these functions being used (Smutty actually provides a form plugin for creating forms). In the url example no controller has been specified, if you miss this out then Smutty will default to the current controller. These exmaples just use the default Smutty parameters (controller, action and id), but of course you can use your own. So if you have a route defined as:
route.post.show = year/month/day
Then you can use these functions with these arguments as you'd expect!
{link url={ action="show" year="2007" month="12" day="1" } text="My Link"}
Displaying Errors
By default Smutty will display errors using it's own templates, but if you like you can over-ride this functionality and supply your own custom error pages.
To use a custom error page just create a template in your application's views folder with the following format:
/views/error-CODE.tpl
Where CODE is the HTTP error code that will be handled by this template (404, 500, etc...)
Links: Post a comment