Reputation: 415
I'm trying to use a template library created by Phil Sturgeon. He has some great documentation for his template library but I've ran into a little bit of a problem with how I have my file structure done.
-views
-layouts
-default.php
-partials
-header_view.php
-footer_view.php
-metadata_view.php
login_view.php
Inside of my login controller index function I have the following code to establish which layout to use and which view to load as the body of the template. I also have included what I have inside of the header_view.php file which I believe is what I'm not getting to work right. I was hoping to be able to target the metadata partial inside of the header_view. According to the documentation I'm doing it correctly, however, I'm getting an error undefined index metadata coming from the header_view.php file. Any light on this issue.
Documention from the github repostitory: https://github.com/philsturgeon/codeigniter-template
$this->template
->title('Login Form')
->set_partial('header', 'partials/header_view')
->set_partial('metadata', 'partials/metadata_view')
->set_partial('footer', 'partials/footer_view')
->set_layout('default')
->build('login_form_view');
default.php
<?php echo $template['partials']['header']; ?>
<?php echo $template['body']; ?>
<?php echo $template['partials']['footer']; ?>
header_view.php
<!DOCTYPE html>
<html>
<head>
<title><?php echo $template['title']; ?></title>
<?php echo $template['partials']['metadata']; ?>
</head>
<body
Upvotes: 0
Views: 776
Reputation: 9007
well im a codeigniter fan; and i honestly think that the real power of CI that it doesnt come loaded with fancy "TEMPLATING lib" or auth lib; yet it give you all tools to build your own swiftly and very rappidly;
so my answer my not be what you really asking for but its a simple example of how you can build your own so called templaling lib in ci in less than 10mins and works flowlessly;
create MY_Controller inside applicaiton/core folder;
class MY_Controller extends CI_Controller {
protected $noEcho=true,$body = 'base/layout', $title = 'YOUR CONTROLLER TITLE',
$js = array(), //filename
$inline_js = '', //script
$css = array(),
$inline_css = '', //style
$content = array(); //html
inside it will be 3 basic functions
1
function output($data,$section='content'){
//checking type of data to be pushed its either array or string(html)
//this is a view it should be formated like this array( viewname,$data )
if( is_array($data) ){
$this->content[ $section ][] = $this->load->view( $data[0], $data[1], TRUE );
return $this;//to allow chaing
}elseif( is_string($data) ){//this is html
$this->content[ $section ][] = $data;
return $this;//to allow chaing
}
}
2nd is a function that let you add js,css and and inline js&css to this page;
function _asset( $link, $txt = FALSE ) {
if ( $txt !== FALSE ) {
if ( $txt == 'js' )
$this->inline_js[] = $txt;
elseif ( $txt == 'css' )
$this->inline_css[] = $txt;
}else{
if ( pathinfo( $link, PATHINFO_EXTENSION ) == 'css' ){
$this->css[] = link_tag( base_url( 'assets/css/' . trim( $link, "/\\" ) ) );
}else{
$this->js[] = '<script src="' . base_url( 'assets/js/' . trim( $link, "/\\" ) ) . '"></script>';
}
}
return $this;
}
and at last a function that put all your parts together;
protected function print_page(){
if ( $this->noEcho ) ob_clean();
$data=array();
$data[ 'title' ] = $this->title;
$data[ 'css' ] = is_array( $this->css ) ? implode( "\n", $this->css ) : '';
$data[ 'js' ] = is_array( $this->js ) ? implode( "\n", $this->js ) : '';
$data[ 'inline_css' ] = ( $this->inline_css ) ? '<style>' . implode( "\n", $this->inline_css ) . '</style>' : '';
$data[ 'inline_js' ] = ( $this->inline_js ) ? implode( "\n", $this->inline_js ) : '';
foreach ( $this->content as $section => $content ) {
$data[ $section ] = is_array( $content ) ? implode( "\n\n\n ", $content ) : $content;
} //$this->content as $section => $content
return $this->load->view( $this->body, $data );
}
now put all three together and extend your controller to this base controller;
now for the example page your are trying to build i would do:
Controller :
public function __construct() {
parent::__construct();
$this->title = 'Controller title';
$this->body = 'base/default';
//load all your assets.. if its across all pages then load them in MY_controller construct
$this->assets('jquery.min.js')
->assets('bootstrap.css')
->assets('alert("itworks");','js');//will be added to $inline_js
}
function index(){
$var = $This->some_model->get_data();
$this->output( array('some_View',$var) )
->output('<hr/>')
->output('THIS WILL BE IN $FOOTER','footer')
->print_page();
}
Now how clean is that :) ?;
now your controller will load view set on this->body
and all pass all your sections to it.
so for above example your view will recieve 2 variables.
at end i hope that i hope this small demo help you understand the unlimited possibilities that these 3 small functions can do thanks to CI native view loader.
Upvotes: 2