Store HTML View File In A Variable

05 Sep

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.

    <h1><?php echo $title; ?></h1>

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

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.

    $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 theoutput 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

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().


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.

* Get the view file
public function getView($viewFile, $vars = NULL)
    // Check the view file exists
         return false;

    // Extract the variables to be used by the view


    include_once $viewFile;

    $view = ob_get_contents();


    return $view;



Posted by on September 5, 2013 in HTML


6 responses to “Store HTML View File In A Variable

  1. Pingback: chat
  2. Pingback: Chat
  3. Dona Killen

    September 10, 2013 at 3:38 pm

    Hello very cool tangle site!! Guy .. Striking .. Amazing .. I will bookmark your website and assume the feeds also…I am blissful to attain frequent useful in sequence proper at this point in the suggest, we want work made known extra strategies in this regard, thankfulness for sharing. . . . . .

  4. define online marketing

    September 12, 2013 at 8:00 pm

    Attractive section of content. I just stumbled upon your web site and in accession
    capital to assert that I get in fact enjoyed account your blog posts.

    Anyway I will be subscribing to your feeds and even I achievement you access consistently fast.

  5. apprenticeships for electricians

    September 13, 2013 at 1:36 am

    I’m impressed, I have to admit. Seldom do I encounter a blog that’s both equally educative and engaging, and without
    a doubt, you have hit the nail on the head.
    The issue is something which too few people are speaking intelligently about.
    I am very happy I found this during my search for something regarding

  6. winning numbers florida lottery

    September 17, 2013 at 2:55 pm

    I love looking through an article that can
    make men and women think. Also, thank you for permitting me
    to comment!


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: