$this->view = new SuperView; // SuperView extends view, constructor takes care of core CSS, JS, metatags, titling, etc
$this->view->add_css('custom.css') // add anything else you like
views/templates/layout.php
<html>
<head>
// create title, meta, css, and js tags etc.
</head>
<body>
<div class="header">
Header Stuff
</div>
<div class="content">
<?php echo $content ?>
</div>
<div class="footer">
Footer Stuff
</div>
</body>
</html>
class Controller_Welcome extends Controller
{
public function action_index()
{
$view = new SuperView('templates/layout');
$view->content = View::factory('pages/welcome');
echo $view;
}
}
echo SuperView::factory('optional/path/to/file.php')->add_css('an/optional/css/file.css')->render();
<?php<br />
/**
* Template View
*
* The Template View class sets up a variety of core page elements, such as
* page title, adding scripts, stylesheets, and groups of assets from the
* "assets" configuration file.
*
* The variables $title, $css, and $scripts are assumed to be set in the template.
*
*/
class Template_View extends View
{
// --------------------------------------------------------------------------------------------------
// VARIABLES
// --------------------------------------------------------------------------------------------------
// template and auto-render
protected $template = 'template';
protected $auto_render = TRUE;
// default page variables
public $title = NULL;
// scripts and css arrays (these are converted to strings upon rendering)
public $scripts = array();
public $css = array();
// --------------------------------------------------------------------------------------------------
// INITIALIZE
// --------------------------------------------------------------------------------------------------
/**
* Template_View constructor
*/
public function __construct($template = NULL, $data = NULL)
{
// default path
if($template == NULL)
{
$template = $this->template;
}
// default title
if($data == NULL)
{
$data = $this->title;
}
if(is_string($data))
{
$data = array('title' => $data);
}
// constructor
parent::__construct($template, $data);
// auto-render
if ($this->auto_render == TRUE)
{
Event::add('system.post_controller', array($this, '_render'));
}
}
/**
* Chainable method to create an instance of Template_View
*/
public static function factory($template = NULL, $data = NULL)
{
return new Template_View($template, $data);
}
// --------------------------------------------------------------------------------------------------
// CORE PROPERTIES
// --------------------------------------------------------------------------------------------------
/**
* Chainable method to change the view template file
*/
public function set_template($template)
{
$this->set_filename($template);
return $this;
}
/**
* Chainable method to set the title variable of the view
*/
public function set_title($value)
{
$this->title = $value;
return $this;
}
/**
* Load single or multiple stylesheets, searching in the paths set up in the assets configuration file
*/
// --------------------------------------------------------------------------------------------------
// ASSETS
// --------------------------------------------------------------------------------------------------
public function css($file)
{
if(is_array($file))self::css($file);
else $this->css[$file] = html::stylesheet(Kohana::config('assets.paths.css') . $file);
return $this;
}
/**
* Load single or multiple scripts, searching in the paths set up in the assets configuration file
*/
public function script($file)
{
if(is_array($file))self::script($file);
else $this->scripts[$file] = html::script(Kohana::config('assets.paths.scripts') . $file);
return $this;
}
/**
* Load groups of assets specified by values set in the assets configuration file
*/
public function assets($key)
{
$assets = Kohana::config('assets.assets.' . $key);
if($assets != NULL)
{
if(array_key_exists('css', $assets))
{
foreach($assets['css'] as $file)
{
$this->css($file);
}
}
if(array_key_exists('scripts', $assets))
{
foreach($assets['scripts'] as $file)
{
$this->script($file);
}
}
}
return $this;
}
// --------------------------------------------------------------------------------------------------
// CORE
// --------------------------------------------------------------------------------------------------
/**
* Set all stored variables in the view and render
*/
public function render($print = FALSE)
{
// set variables
$this
->set('title', htmlentities($this->title))
->set('css', implode('', $this->css) . "\n")
->set('scripts', implode('', $this->scripts) . "\n");
// render
parent::render($print);
}
/**
* Callback for auto-rendering
*/
public function _render()
{
if ($this->auto_render == TRUE)
{
$this->render(TRUE);
}
}
/**
* Override parent class to magically set view variables
*
* @param string variable key
* @param string variable value
* @return void
*/
public function __set($key, $value)
{
$protected = array('template', 'auto_render');
if(!in_array($key, $protected))
{
$this->kohana_local_data[$key] = $value;
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $title; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
echo $css;
echo $scripts;
echo $editable;
?>
</head>
<body style="margin-bottom:100px; clear:both; background-color:#bbd1e5;">
<div id="body" style="margin-bottom:100px; clear:both"">
<div id="header">
<?php
echo $header;
?>
</div>
<div id="nav-container" style="height:50px;">
<?php
$nav->set('class', 'menu horizontal interactive dropdown')->render('top-nav', TRUE);
?>
</div>
<div id="content" class="<?php echo count(Router::$segments) ? Router::$segments[0] : 'home';?>">
<?php
echo $content;
if(isset($sidebar))
{
$sidebar->render(TRUE);
}
?>
</div>
<div class="clear"></div>
<div id="footer">
<p style="float:right">© Copyright Pacific <?php echo date('Y'); ?>. All rights reserved</p>
<?php
$nav->set('class', 'horizontal')->render('foot-nav', TRUE);
?>
</div>
</div>
</body>
</html>
It looks like you're new here. If you want to get involved, click one of these buttons!