Views contain the HTML markup. In a typical MVC framework data variables are passed to view file and it is upto the view file how to display them. Like say an array of user profiles would be passed and the view file may display the list in a ul li or in tr td format.
Views in PHPasap reside in
app/views/ directory. So suppose there is a file named
welcome.php in this directory, then to use this view file in any of your Controller method you will use the below syntax
$view = View::render("welcome"); //render returns string containing html markup in welcome.php return $view; //OR return View::make("welcome"); //make returns an object of View_Handler class
Remember you have to return the view string or View_Handler object and not echo it.
The second argument to make() and render() method is an optional associative array whose keys will be passed as variables with corresponding value as the vaue of variable. See example below
return View::render("welcome", ["var_1"=>"value_1"]); //OR return View::make("welcome", ["var_1"=>"value_1"]);
Sometimes you may want to seperate out the header and footer templates so that they can be used in common in all view files. Below is how you would do.
$header = View::render("header"); //remember to use render() which returns string and NOT make() which returns obj return View::make("welcome", ["header"=>$header]);
Below too is a legal example. Here in a view file we call another view
<?php echo View::render("header"); ?> <div class="container"> </div> <?php echo View::render("footer"); ?>
The path passed to
render() method is always relative to
views folder. Even if you are calling these methods in a view file or in a Controller the path will always be relative to the views folder.
Suppose you are using below file and directory structure in
Then to access any of the above view files you may use below syntax
View::make("template/full-width"); //for app/views/template/full-width.php View::make("modules/welcome"); //for app/views/modules/welcome.php