Views
In NipIgniter, you can load view with layout or without layout.
Creating a Layout
Using your text editor, create a file called layout.php, and put this in it:
You can add many variable and can be used in the layout. But the important variable you should be placed on the layout is $pageContent. This variable will show a partial view.
Creating a View
Using your text editor, create a file called blog_view.php, and put this in it:
Then save the file in your application/views/ folder.
Loading a View
To load a particular view file you will use the following function:
class BlogController extends Nip_Controller{
public $pageLayout = "layout";
public function index(){
$this->render("blog_view");
}
}
Where blog_view is the name of your view file. Note: The .php file extension does not need to be specified unless you use something other than .php.
If you visit your site using the URL you did earlier you should see your new view. The URL was similar to this:
example.com/blog/
Storing Views within Sub-folders
Your view files can also be stored within sub-folders if you prefer that type of organization. When doing so you will need to include the folder name loading the view. Example:
$this->render('folder_name/file_name');
Adding Dynamic Data to the View
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading function. Here is an example using an array:
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->render('blogview', $data);
And here's an example using an object:
$data = new Someclass();
$this->render('blogview', $data);
Note: If you use an object, the class variables will be turned into array elements.
Let's try it with your controller file. Open it add this code:
Now open your view file and change the text to variables that correspond to the array keys in your data:
Then load the page at the URL you've been using and you should see the variables replaced.
Returning views as data
There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data. The default behavior is false, which sends it to your browser. Remember to assign it to a variable if you want the data returned:
$string = $this->render('myfile', array(), TRUE);