Views

The Stratos Framework does not require you to use a specific template engine to define your views. Currently, you can use either plain PHP scripts or Smarty templates to create your views.

PHP Views

Creating views using plain PHP scripts is the easiest because it does not require you to learn any additional template engine syntax, and you have the full power of PHP at your disposal. To create a view using plain PHP, simply create a new PHP script with your presentation logic, and save it to the views directory of your application. Typically, the name of your view script will follow the naming convention ClassName.methodName.php. For example, suppose I defined the following class in my actions/ directory:

actions/SomeController.php

<?php

class SomeController
{
    function doSomething()
    {
        return array('stuff' => 'blah');
    }
}

?>

The action defined above would automatically look for a view called SomeController.doSomething.php in the views/ directory of your application. The view might look like this:

views/SomeController.doSomething.php

<p>The action sent <?= $stuff ?> to this view.</p>

If you do not want to use the naming convention described above, you can use the Stratos::setView() method to specify the name of the view that you would like your action to use. For instance, we could modify the code above to use a view called randomViewName.php:

actions/SomeController.php

<?php

class SomeController
{
    function doSomething()
    {
        Stratos::setView('randomViewName.php');
        return array('stuff' => 'blah');
    }
}

?>

The action defined above would then look for a view called randomViewName.php in the views/ directory of your application. The view might look like this:

views/randomViewName.php

<p>
  Hi, I am view #2. I am rebellious and I refuse to follow conventions.
  Nevertheless, I still know the action sent <?= $stuff ?> to this view.
</p>

Using Smarty Templates

As of version 0.92, Stratos is packaged with a view controller for the Smarty Template Engine. Some developers argue that using a template engine like Smarty results in cleaner views -- it all depends on what you prefer. By default, Stratos will use the Smarty view controller to display views that have a *.tpl extension. Although Stratos comes bundled with a Smarty view controller, there are a couple of prerequisites before you can use Smarty templates in your application. First, Stratos does not come bundled with the Smarty template engine itself. Therefore, you will first need to download a copy of the Smarty source code and place it in a directory called smarty/ in the includes/ directory of your application. Second, Smarty uses a directory called templates_c to save compiled versions of your templates, so you will need to create this directory in the base directory of your application. To support Smarty templates, your application's directory structure and scripts would be organized like this this:

actions/
  SomeController.php
conf/
includes/
  smarty/
stratos/
templates_c/
views/
  SomeController.doSomething.tpl

Last, if you want your view to default to use the ".tpl" extension rather than ".php", you'll have to go to your toolkits under Stratos - Configure - General and change the default view option from "{plugin:}{subdir}{class}.{method}.php" to "{plugin:}{subdir}{class}.{method}.tpl".

Specifically, if you wanted to use Smarty to define the view for the action that was shown earlier, you would create a script SomeController.doSomething.tpl. Note: Smarty does not support the ability to call static methods from within templates, so if you want to leverage functions like Stratos::makeUrl(), you will need to use the instance name $stratos instead. Below is an example of a view that uses Smarty:

<a href="{$stratos->makeUrl('Test/test')}">testing make url</a>

<table>
{section name=key loop=$posts}
   <tr bgcolor="{cycle values="#eeeeee,#dddddd"}">
      <td>{$posts[key]->title}</td>
   </tr>
{/section}
</table>

Using Other Template Engines

If you do not wish to use either plain PHP scripts or Smarty templates to create views, you can create your own view controller by extending the ViewController class. The ViewController class is defined in ViewController.php, which is in the stratos/ directory where you installed the framework. Because the ViewController class is abstract, the actual implementation of the ViewController::execute() method is left to child classes. This means that ViewController does not enforce any particular type of view scripts, so each ViewController implementation may execute different types of scripts. For example, the ViewController_Script class expects view scripts to be PHP files, whereas another implementation might be expecting a template for some templating engine.

If you wanted to use the Savant template system, for instance, you could create a class named ViewController_Savant that extends ViewController. You would then save the file as ViewController_Savant.php in the same directory as ViewController.php. In your new class (ViewController_Savant), you would implement the execute() method, which would contain the code necessary to work with Savant templates. Once the ViewController_Savant class was complete, you would then need to tell Stratos to use your newly created view controller. To do this, you will need to edit the configuration file by hand. Locate the $config['Stratos']['view-controllers'] key and add another element specifying your view controller's class name and an expression to match scripts that should be processed using this view controller. For example, your view controller configuration might look like this (Note: other options removed for readability):

<?php

$config = array(
    'Stratos' => array(
        'view-controllers' => array(
            0 => array(
                'class' => 'stratos/ViewController_Script.php',
                'match' => '/.*\\.php$/',
            ),
            1 => array(
                'class' => 'stratos/ViewController_Smarty.php',
                'match' => '/.*\\.tpl$/',
            ),
            2 => array(
                'class' => 'stratos/ViewController_Savant.php',
                'match' => '/.*\\.svt$/',
            ),
        )
    )
);

?>

If you are seriously considering creating your own view controller for Stratos, use the source code in ViewController_Smarty.php or ViewController_Script.php as a reference.

Copyright © 2006-2007 Sephira Software, LLC. Be sure to check out Mashfest, a music festival site that we are working on.