Guest User

Untitled

a guest
Jan 24th, 2018
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.09 KB | None | 0 0
  1. <?php  
  2. if (!defined('BASEPATH')) exit('No direct script access allowed');
  3.  
  4. /**
  5.  *  Layout class for creating default page layouts.
  6.  *
  7.  *  This class will allow you to use a layout file so you won't need to include
  8.  *  the consistent blocks of code throughout your template files.
  9.  *
  10.  *  @author The Sweeney <jfmike@gmail.com>
  11.  */
  12. class Layout Extends CI_Controller {
  13.   /**
  14.    *  The controller object for the calling class
  15.    *
  16.    *  @var object
  17.    *  @access private
  18.    */
  19.   private $obj;
  20.  
  21.   /**
  22.    *  The layout filename (with or w/out the '.php' at the end)
  23.    *
  24.    *  @var string
  25.    *  @access private
  26.    */
  27.   private $layout;
  28.  
  29.   /**
  30.    *  The default controllers to include. It can be overwritten if necessary.
  31.    *
  32.    *  @var array
  33.    *  @access public
  34.    */
  35.   public $defaults = array('header','footer');
  36.  
  37.   /**
  38.    *  Create an Instance, optionally deciding which layout to use.
  39.    *
  40.    *  @param string $layout A string that points to the default layout file.
  41.    *                        Defaults to /layouts/layout_main.php which will be located in /application/views.
  42.    */
  43.   public function __construct($layout = "/layouts/layout_main") {
  44.     $this->obj =& get_instance();
  45.     $this->layout = $layout;
  46.   }
  47.  
  48.   /**
  49.    *  Set the layout to use a different layou than the default main layout
  50.    *
  51.    *  @param string $layout A string that points to the location to the layout file you wish to use.
  52.    */
  53.   public function setLayout($layout) {
  54.     $this->layout = $layout;
  55.   }
  56.  
  57.   /**
  58.    *  A semi-recursive method to load a view inside the layout.
  59.    *
  60.    *  @param string $view location to view file
  61.    *  @param array $data The data to load into the view file
  62.    *  @param booleanl $return A boolean to let the method know whether or not to call itself or stop processing.
  63.    *
  64.    *  @return string $output The html output string
  65.    *
  66.    *  @see CI_Loader::view()
  67.    */
  68.   public function view($view, $data=null, $return=false) {
  69.     $loadedData = array();
  70.     $loadedData['content'] = $this->obj->load->view($view,$data,true);
  71.    
  72.     if($return) {
  73.       $output = $this->obj->load->view($this->layout, $loadedData, true);
  74.       return $output;
  75.     } else {
  76.       $this->obj->load->view($this->layout, $loadedData, false);
  77.     }
  78.   }
  79.  
  80.   /**
  81.    *  A replacement for the view function if the CI templating classes are being used
  82.    *
  83.    *  @param string $template The template to load.
  84.    *  @param array $data The array of data to load into the file to be parsed
  85.    *  @param boolean $return A boolean to let the method know whether or not to call itself or stop processing.
  86.    *
  87.    *  @return string $output The html output string
  88.    *
  89.    *  @see Layout::view()
  90.    */
  91.   public function parse($template, $data=null, $return=FALSE) {
  92.     $loadedData = array();
  93.     $loadedData['content'] = $this->obj->parser->parse($template,$data,true);
  94.  
  95.     if ($return) {
  96.       $output = $this->obj->parser->parse($this->layout, $loadedData, true);
  97.       return $output;
  98.     } else {
  99.       $this->obj->parser->parse($this->layout, $loadedData, false);
  100.     }
  101.   }
  102. }
Add Comment
Please, Sign In to add comment