Advertisement
Guest User

Untitled

a guest
Feb 28th, 2011
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.03 KB | None | 0 0
  1. <?
  2.  
  3. /**
  4.  * MY_Template
  5.  *
  6.  * @package MY_Template
  7.  * @author mk
  8.  * @copyright 2011
  9.  * @version 1.0
  10.  * @access public
  11.  */
  12. class MY_Template {
  13.    
  14.     private $CI;
  15.    
  16.     private $layout;
  17.     private $partials = array();
  18.     private $destinations = array();
  19.    
  20.     function __construct() {
  21.         $this->CI =& get_instance();
  22.     }
  23.    
  24.     /**
  25.      * MY_Template::set_layout()
  26.      *
  27.      * Define el view para el layout
  28.      *
  29.      * @param string $layout
  30.      * @return void
  31.      */
  32.     public function set_layout($layout) {
  33.         $this->layout = $layout;
  34.     }
  35.    
  36.     /**
  37.      * MY_Template::set_view()
  38.      *
  39.      * Escribir un View en una zona del template
  40.      *
  41.      * @param string $destination
  42.      * @param string $view_name
  43.      * @param array $view_data
  44.      * @return void
  45.      */
  46.     public function set_view($destination = null, $view_name = null, $view_data = array()) {
  47.         if (isset($destination) && isset($view_name)) {
  48.             $this->partials[] = array(
  49.                 "type" => "view",
  50.                 "destination" => $destination,
  51.                 "view_name" => $view_name,
  52.                 "view_data" => $view_data
  53.             );
  54.         } else {
  55.             show_error("Destination y View Name deben ser especificados");
  56.         }
  57.     }
  58.    
  59.     /**
  60.      * MY_Template::set_value()
  61.      *
  62.      * Escribir un valor en una zona del template
  63.      *
  64.      * @param string $destination
  65.      * @param mixed $value
  66.      * @return void
  67.      */
  68.     public function set_value($destination = null, $value = null) {
  69.         if (isset($destination) && isset($value)) {
  70.             $this->partials[] = array(
  71.                 "type" => "value",
  72.                 "destination" => $destination,
  73.                 "value" => $value
  74.             );
  75.         } else {
  76.             show_error("Destination y Value deben ser especificados");
  77.         }
  78.     }
  79.    
  80.     /**
  81.      * MY_Template::render()
  82.      *
  83.      * Renderear el layout con todos los Views y valores escritos
  84.      *
  85.      * @param bool $return
  86.      * @return void
  87.      */
  88.     public function render($return = false) {
  89.         foreach ($this->partials as $partial) {
  90.             switch ($partial['type']) {
  91.                 case "view":
  92.                     $this->destinations[$partial['destination']] = $this->CI->load->view($partial['view_name'], $partial['view_data'], true);
  93.                     break;
  94.                 case "value":
  95.                     $this->destinations[$partial['destination']] = $partial['value'];
  96.                     break;
  97.             }
  98.         }
  99.         if ($return) {
  100.             return($this->CI->load->view($this->layout, array(), true));
  101.         } else {
  102.             $this->CI->load->view($this->layout);
  103.         }
  104.     }
  105.    
  106.     /**
  107.      * MY_Template::get()
  108.      *
  109.      * Obtener un View rendereado o un valor para escribirlo en el layout
  110.      *
  111.      * @param string $destination
  112.      * @return
  113.      */
  114.     public function get($destination) {
  115.         if (isset($this->destinations[$destination])) {
  116.             return($this->destinations[$destination]);
  117.         } else {
  118.             return("");
  119.         }
  120.     }
  121.    
  122.     public function __get($destination) {
  123.         if (isset($this->destinations[$destination])) {
  124.             return($this->destinations[$destination]);
  125.         } else {
  126.             return("");
  127.         }
  128.     }
  129.    
  130.     public function __set($destination = null, $value = null) {
  131.         if (isset($destination) && isset($value)) {
  132.             $this->destinations[$destination] = $value;
  133.         } else {
  134.             show_error("Destination y Value deben ser especificados");
  135.         }
  136.     }
  137.    
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement