Store an HTML View File in a Variable
Join the DZone community and get the full member experience.
Join For FreeIf you have used an MVC language before you would be familiar with the practice of separating your business logic from your HTML. The way this works in an 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 an HTML file. We will also be able to return the contents of that HTML inside a variable to output this in any place within our application we want.
To achieve this we are going to use the PHP output controller functions.
View File
A typical view file should contain just the HTML for the page. This file should only be used to output contents to the visitor and 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 containing 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 variable we can just pass these into our getView() function.
The getView() function takes two parameters: the first will be the location of our view file and 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 its 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 since 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 an 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 the output and return the contents of the output buffer.
$view = ob_get_contents();
Since we have collected all the output and don't want anything else included in the output of the view file, we then 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 the 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; } ?>
Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Does the OCP Exam Still Make Sense?
-
You’ve Got Mail… and It’s a SPAM!
-
Demystifying SPF Record Limitations
-
How Agile Works at Tesla [Video]
Comments