KumbiaPHP beta2-dev
Framework PHP en español
kumbia_view.php
Ir a la documentación de este archivo.
00001 <?php
00022 class KumbiaView {
00028         protected static $_content;
00029 
00035         protected static $_view;
00036 
00042         protected static $_template = 'default';
00043 
00049         protected static $_response;
00050 
00056         protected static $_path;
00057 
00066         protected static $_cache = array('type' => FALSE, 'time' => FALSE, 'group'=> FALSE);
00067 
00074         public static function select($view, $template = FALSE)
00075     {
00076                 self::$_view = $view;
00077 
00078         // verifica si se indico template
00079                 if($template !== FALSE) {
00080             self::$_template = $template;
00081         }
00082         }
00083 
00089         public static function template($template)
00090     {
00091                 self::$_template = $template;
00092         }
00093 
00103         public static function response($response, $template = FALSE)
00104     {
00105                 if($response == 'view') { //se mantiene pero ya esta deprecated
00106                         self::$_template = NULL;
00107                 } else {
00108             self::$_response = $response;
00109         }
00110 
00111         // verifica si se indico template
00112                 if($template !== FALSE) {
00113             self::$_template = $template;
00114         }
00115         }
00116 
00122         public static function setPath($path)
00123     {
00124                 self::$_path = $path.'/';
00125         }
00126 
00132         private static function getPath()
00133     {
00134                 if(self::$_response && self::$_response != 'view'){
00135                         return self::$_path.self::$_view.'.'.self::$_response.'.phtml';
00136                 }
00137                 return self::$_path.self::$_view.'.phtml';
00138         }
00139 
00145         public static function get($atribute)
00146     {
00147                 return self::${"_$atribute"};
00148         }
00149 
00157         public static function cache($time, $type='view', $group=FALSE) {
00158                 if($time !== FALSE) {
00159                         self::$_cache['type'] = $type;
00160                         self::$_cache['time'] = $time;
00161                         self::$_cache['group'] = $group;
00162                 } else {
00163                         self::$_cache['type'] = FALSE;
00164                 }
00165         }
00166 
00173         public static function render(/*Controller*/ $controller, /*Router*/ $_url)
00174         {
00175         if(!self::$_view && !self::$_template){
00176             return ob_end_flush();
00177         }
00178 
00179         // Mapea los atributos del controller en el scope
00180         extract(get_object_vars($controller), EXTR_OVERWRITE);
00181 
00182                 // inicia contenido con valor nulo
00183                 self::$_content = NULL;
00184 
00185                 // si se encuentra en produccion
00186                 if(PRODUCTION) {
00187                         // si se cachea vista
00188                         if(self::$_cache['type'] == 'view') {
00189                                 // el contenido permanece nulo si no hay nada cacheado o la cache expiro
00190                                 self::$_content = Cache::driver()->get($_url, self::$_cache['group']);
00191                         }
00192                 }
00193 
00194         // carga la vista si no esta en produccion o se usa scaffold o no hay contenido cargado
00195         if(!PRODUCTION || $scaffold || !self::$_content) {
00196             // Carga el contenido del buffer de salida
00197             self::$_content = ob_get_clean();
00198 
00199             // Renderizar vista
00200             if($view = self::$_view) {
00201                 ob_start();
00202 
00203                 $file = APP_PATH.'views/'.self::getPath();
00204                 if(!is_file($file) && $scaffold) {
00205                     $file = APP_PATH ."views/_shared/scaffolds/$scaffold/$view.phtml";
00206                 }
00207 
00208                 // carga la vista
00209                 if (!include $file) throw new KumbiaException('Vista "' . self::getPath() . '" no encontrada');
00210 
00211                                 // si esta en produccion y se cachea la vista
00212                                 if(PRODUCTION && self::$_cache['type'] == 'view') {
00213                                     Cache::driver()->save(ob_get_contents(), self::$_cache['time'], $_url, self::$_cache['group']);
00214                                 }
00215 
00216                 // Verifica si hay template
00217                         if(!self::$_template) {
00218                                 ob_end_flush();
00219                                 return;
00220                         }
00221 
00222                         self::$_content = ob_get_clean();
00223                         }
00224                 } else {
00225             ob_clean();
00226         }
00227 
00228         // Renderizar template
00229                 if($template = self::$_template) {
00230                         ob_start();
00231 
00232                         // carga el template
00233                         if (!include APP_PATH . "views/_shared/templates/$template.phtml") throw new KumbiaException("Template $template no encontrado");
00234 
00235                         // si esta en produccion y se cachea template
00236                         if(PRODUCTION && self::$_cache['type'] == 'template') {
00237                                 Cache::driver()->save(ob_get_contents(), self::$_cache['time'], $_url, "kumbia.templates");
00238                         }
00239 
00240                         return ob_end_flush();
00241                 }
00242 
00243                 echo self::$_content;
00244         }
00245 
00250         public static function content()
00251         {
00252                 if(isset($_SESSION['KUMBIA.CONTENT'])){
00253                         echo $_SESSION['KUMBIA.CONTENT'];
00254                         unset($_SESSION['KUMBIA.CONTENT']);
00255                 }
00256                 echo self::$_content;
00257         }
00258 
00268         public static function partial($partial, $__time=FALSE, $params=array())
00269         {
00270                 if(PRODUCTION && $__time && !Cache::driver()->start($__time, $partial, 'kumbia.partials')) {
00271                     return;
00272                 }
00273 
00274                 //Verificando el partials en el dir app
00275                 $__file = APP_PATH . "views/_shared/partials/$partial.phtml";
00276 
00277                 if(!is_file($__file)){
00278                     //Verificando el partials en el dir core
00279                         $__file = CORE_PATH . "views/partials/$partial.phtml";
00280                 }
00281 
00282                 if(is_string($params)) {
00283                         $params = Util::get_params($params);
00284                 }
00285 
00286                 // carga los parametros en el scope
00287                 extract ($params, EXTR_OVERWRITE);
00288 
00289                 // carga la vista parcial
00290                 if (!include $__file) throw new KumbiaException('Vista Parcial "'.$__file.'" no se encontro');
00291 
00292                 // se guarda en la cache de ser requerido
00293         if(PRODUCTION && $__time) {
00294             Cache::driver()->end();
00295         }
00296 
00297         }
00298 
00305     public static function helpers ($helper)
00306     {
00307         $helper = Util::smallcase($helper);
00308         $path = "extensions/helpers/$helper.php";
00309         $file = APP_PATH . $path;
00310 
00311         if (! is_file($file)) {
00312             if (!include_once CORE_PATH . $path) throw new KumbiaException("Helpers $helper no encontrado");
00313             return;
00314         }
00315 
00316         require_once $file;
00317     }
00318 }
00319 
00328 function h($s, $charset = APP_CHARSET) {
00329 
00330     return htmlspecialchars($s, ENT_QUOTES, $charset);
00331 }
00332 
00341 function eh($s, $charset = APP_CHARSET) {
00342 
00343     echo htmlspecialchars($s, ENT_QUOTES, $charset);
00344 }
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Enumeraciones