KEMBAR78
Codeigniter : Two Step View - Concept Implementation | PDF
CodeIgniter
Two Step View Concept Implementation




                        Abdul Malik Ikhsan
         http://slideshare.net/samsonasik
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 )
Two Step View, a simple
            thought
• Get View Content rendered
• Then, `copy` the content into the html
  layout
Mention the ‘components’ needed
• Hook ~> initialize
• Helper ~> ‘generate’ two step view
• Layout View ~> ‘echo-ing’ it
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 */
Initializing ( 2 )

$hook['pre_controller'][] = array(
'class' => 'Initializetwostepview',
'function' => 'initialize',
    'filename' => 'Initializetwostepview.php',
      'filepath' => 'hooks',
     //      'params' => array()
 );
/* End of file hooks.php */
/* Location: ./application/config/hooks.php */
<?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 */
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 */
The content
<p class=“article”> this is test view</p>

<?php
/* End of file test_view.php */
/* Location: ./application/view/test_view.php */
‘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 */
Show Time ;)


           With output buffering




          Without output buffering
Terima Kasih
This presentation contained copyrighted material licensed under various
creative commons licenses unless otherwise noted:


References
•   CodeIgniter User Guide
•   http://martinfowler.com/eaaCatalog/twoStepView.html
•   http://slideshare.net/samsonasik
•   http://samsonasik.wordpress.com

Codeigniter : Two Step View - Concept Implementation

  • 1.
    CodeIgniter Two Step ViewConcept Implementation Abdul Malik Ikhsan http://slideshare.net/samsonasik
  • 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 */
  • 6.
    Initializing ( 2) $hook['pre_controller'][] = array( 'class' => 'Initializetwostepview', 'function' => 'initialize', 'filename' => 'Initializetwostepview.php', 'filepath' => 'hooks', // 'params' => array() ); /* End of file hooks.php */ /* Location: ./application/config/hooks.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 extendsCI_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 theLayout <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
  • 12.
  • 13.
    This presentation containedcopyrighted material licensed under various creative commons licenses unless otherwise noted: References • CodeIgniter User Guide • http://martinfowler.com/eaaCatalog/twoStepView.html • http://slideshare.net/samsonasik • http://samsonasik.wordpress.com