2. Two Step View
• Turns domain data into HTML in two
steps: first by forming some kind of logical
page, then rendering the logical page into
HTML. ( Martin Fowler )
3. Two Step View, a simple
thought
• Get View Content rendered
• Then, `copy` the content into the html
layout
4. Mention the ‘components’ needed
• Hook ~> initialize
• Helper ~> ‘generate’ two step view
• Layout View ~> ‘echo-ing’ it
5. Initializing ( 1 )
<?php
class Initializetwostepview
{
public function __construct() { }
public function initialize()
{
ob_start(); //Turn on output buffering
}
}
/* End of file Initializetwostepview.php */
/* Location: ./application/hooks/Initializetwostepview.php */
7. <?php
‘Generate’
if ( ! function_exists('show'))
{
function show($view,$data)
{
//get content before view rendered
$beforeshow = ob_get_clean(); // ~ Get current buffer contents
//Assigning by reference allows you to use the original CodeIgniter object
//rather than creating a copy of it. $this is for model,view,and controller only ;)
$ci = & get_instance();
//combine into one string ;)
$stringcontent = $beforeshow.$ci->load->view($view, $data, TRUE);
//set datacontent ;)
$datacontent['content'] = $stringcontent;
//load layout view ;)
$ci->load->view('layout', $datacontent);
}
}
/* End of file layout_helper.php */
/* Location: ./application/helpers/layout_helper.php */
8. Testing
<?php
class Test extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('layout');
}
public function index()
{
$data = array();
$view = 'test_view';
echo '<b><font color="red">hello, i'm showed inside the view layout ;)</font></b> <br />';
show($view,$data);
}
}
/* End of file test.php */
/* Location: ./application/controllers/test.php */
9. The content
<p class=“article”> this is test view</p>
<?php
/* End of file test_view.php */
/* Location: ./application/view/test_view.php */
10. ‘Echo-ing’ in the Layout
<html>
<head>
<title>CI with Layout</title>
</head>
<body>
<h1> Header</h1>
<div id="content">
<?php echo $content; ?>
</div>
<h1>Footer</h1>
</body>
</html>
<?php
/* End of file layout.php */
/* Location: ./application/view/layout.php */
11. Show Time ;)
With output buffering
Without output buffering