Spawn Framework · Contact · Community · Tutorial · Documentation · Download · About

Learn :: View

In this lesson I show you how to use View in project.

View files we saved in /application/view/ and hes end of .phtml if we use php in view or .html.
Create now first view:
/Application/View/home.phtml
<html>
  <head>
    <title>Test</title>
  </head>
<body>
 <?=$body;?>
</body>
</html>
To use view file we have special class in library: sf_view,
use it to send params , helpers and parse view file.
Example: use sf_view to display home.phtml
namespace Controller;
use Spawn\View;
class Home extends \Spawn\Controller
{
   //first action
   public function indexAction()
   {
       $view = new View('home');
       $view -> body = 'Hello View!';
       $this -> response = $view->render(); 
   }
}
Ok, its work. But what we can use view in view?
Whats when we need create html container and get to him other view files?
Create in /Application/View/ new folder 'Home', in this folder will create new views for controller Home.
OK, create index.phtml in /Application/View/Home/:
Hello in index.phtml!
Now we load this file to home.phtml:
namespace Controller;
use Spawn\View;
class Home extends \Spawn\Controller
{
   //first action
   public function indexAction()
   {
       $view = new View('home');
       // if name not isset, view use controller and action name
       $view -> body = new View(); //Home/index 
       //View use __toString to call render()
       $this -> response = $view; 
   }
}
Now see how we can use \Spawn\Controller\Template:
namespace Controller;
class Home extends \Spawn\Controller\Template
{
   public $tmp = 'home';

   public function indexAction()
   {
      // $this -> view hava new View('home')
      $this -> view -> body = new View();
   }
}
In Controller\Teplate $this -> view is rendered in end().