Paulund

Store HTML View File In A Variable

If you have used a MVC language before you would be familiar with the practice of separating your business logic from your HTML. The way this works in MVC language is that the framework will allow you to assign a view (HTML) to a controller (class). When the controller is instantiated it will have an action to display the view with the HTML for the page. Included with the separation is the ability to send variables from your controller to use in your view.

Laravel Framework

An example of how this works on the Laravel framework is to use the method make on the View class.


return View::make('greeting', array('name' => 'Taylor'));

This will be sent to a view called greeting with a variable of name populated with taylor.


<!-- View stored in app/views/greeting.php -->

<html>
    <body>
        <h1>Hello, <?php echo $name; ?></h1>
    </body>
</html>

Laravel Views In this tutorial we are going to re-create this functionality and send variables that can be used in a HTML file. We will also be able to return the contents of that HTML inside a variable to output this in any place we want within our application. To achieve this we are going to use the PHP output controller functions. ## View File

A typical view file should content just the HTML for the page, this file should only be used to output contents to the visitor, there should not be any logic in the View. All the logic in your application should be done in the controller and this will be sent to the view. Below is a simple example of a view file it contains the title and the content of the page.


<div class="view-file">
    <h1><?php echo $title; ?></h1>

    <p><?php echo $content; ?></p>
</div>

Calling View File

When we are calling our view file we are first going to create an array of variables that can be passed to our view. The key of these array values will be turned into variable names in our view. As we only had a title and content variables we can just pass these into our getView() function. The getView() function takes two parameters, first will be the location of our view file, the second will be the variables sent to the view file.


<?php
    $vars = array();
    $vars['title'] = 'Page Title';
    $vars['content'] = 'This is an example of displaying content in the view file';

    return $this->getView('view_file.php', $vars);
?>

Get View Function

Creating the method to get the view files and return it's content is made easy by using the output control functions built into PHP. First we start off by making sure that the view file exists, if the view file doesn't exist then we can't continue with the function so we just return false at that stage. Next we can take the array of variables and use the extract() function that will turn the array keys into variables.


// Extract the variables to be used by the view
if(!is_null($vars))
{
    extract($vars);
}

We need to include the view file into the script so we can use these variables but as we want to return the content of the view file we need to stop PHP from outputting the content of the file, to do this we start the output buffer using ob_start().


ob_start();

With the output buffer started we include the view file, which means it will have access to the variables we have created using the extract() function.


include_once $viewFile;

The view file is not output because we started the ob_start(), to get the content of what is in the buffer we need to use another output control function ob_get_contents(). This will get what would be output and return the contents of the output buffer.


$view = ob_get_contents();

As we have collected all the output and don't want anything else included in the output of the view file then we need to stop the output buffer by using the function ob_end_clean(). Finally we can return the contents of the view file with the HTML populated with the variables we sent to it. Below is full getView() method.


<?php
/**
* Get the view file
*/
public function getView($viewFile, $vars = NULL)
{
    // Check the view file exists
    if(!file_exists($viewFile))
    {
         return false;
    }

    // Extract the variables to be used by the view
    if(!is_null($vars))
    {
        extract($vars);
    }

    ob_start();
 
    include_once $viewFile;
 
    $view = ob_get_contents();
 
    ob_end_clean();
 
    return $view;
}
?>