Created
August 5, 2011 12:30
-
-
Save fabriziomachado/1127428 to your computer and use it in GitHub Desktop.
application/hook/Layout.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); | |
class Layout | |
{ | |
public $base_url; | |
public function initx(){ | |
return 'ok!'; | |
} | |
/** | |
* Metodo que executa as implementacoes. | |
* Este metodo e chamado atraves do arquivo hooks.php | |
* na pasta config. | |
* | |
* @return | |
*/ | |
public function init() | |
{ | |
$CI =& get_instance(); | |
$CI->load->library('parser'); | |
$this->base_url = $CI->config->slash_item('base_url'); | |
// Pegando a saida que o CI gera normalmente. | |
$output = $CI->output->get_output(); | |
if(!isset($output) && $CI->view) | |
{ | |
//@TODO: Tornar default a chamada das viewas | |
$class = $CI->router->class; | |
$method = $CI->router->method; | |
// Pegando o valor de title, se definido no controller. | |
$data = (isset($CI->data)) ? $CI->data : array(); | |
$CI->load->view("$class/$method", $data); | |
$output = $CI->output->get_output(); | |
} | |
// Pegando o valor de title, se definido no controller. | |
$title = (isset($CI->title)) ? $CI->title : ''; | |
// Links CSS definidos no controlador. | |
$css = (isset($CI->css)) ? $this->createCSSLinks($CI->css) : ''; | |
// Links JS definidos no controlador. | |
$js = (isset($CI->js)) ? $this->createJSLinks($CI->js) : ''; | |
// Se layout estiver definido e a regexp nao bater. | |
if (isset($CI->layout) && !preg_match('/(.+).php$/', $CI->layout)) | |
{ | |
$CI->layout .= '.php'; | |
} | |
else | |
{ | |
$CI->layout = 'default.php'; | |
} | |
// Definindo caminho completo do layout. | |
$layout = LAYOUTPATH . $CI->layout; | |
// Se o layout for diferente do default, e o arquivo nao existir. | |
if ($CI->layout !== 'default.php' && !file_exists($layout)) | |
{ | |
// Exibe a mensagem, se o layout for diferente de '.php'. | |
if ($CI->layout != '.php') show_error("You have specified a invalid layout: " . $CI->layout); | |
} | |
// Se o arquivo layout existir. | |
if (file_exists($layout)) | |
{ | |
// Carrega o conteudo do arquivo. | |
$layout = $CI->load->file($layout, true); | |
$data = array( | |
'title_for_layout' => $title, | |
'css_for_layout' => $css, | |
'js_for_layout' => $js, | |
'content_for_layout' => $output | |
); | |
$view = $CI->parser->parse('layouts/default.php', $data); | |
} | |
else | |
{ | |
$view = $output; | |
} | |
$CI->output->set_output($view); | |
$CI->output->_display(); | |
} | |
private function createCSSLinks($links) | |
{ | |
$html = ""; | |
if( is_array($links)) | |
{ | |
for ($i = 0; $i < count($links); $i++) | |
{ | |
$html .= "<link rel='stylesheet' type='text/css' href='" . $this->base_url . CSSPATH . $links[$i] . ".css' media='screen' />\n"; | |
} | |
}else{ | |
$html .= "<link rel='stylesheet' type='text/css' href='" . $this->base_url . CSSPATH . $links . ".css' media='screen' />\n"; | |
} | |
return $html; | |
} | |
private function createJSLinks($links) | |
{ | |
$html = ""; | |
for ($i = 0; $i < count($links); $i++) | |
{ | |
$html .= "<script type='text/javascript' src='" . $this->base_url . JSPATH . $links[$i] . ".js'></script> \n"; | |
} | |
return $html; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment