Skip to content
tomexsans edited this page Apr 12, 2018 · 5 revisions

Template.php @author: Rhustum Evaristo

dependencies

  • anonymous functions
  • array dereferencing

Uses the DIRECTORY/CONTROLLER/METHOD to group your views

SAMPLE:

With Directory URL 1: example.com/dir/class/method Template::load() will find the pages on views/pages/dir/class/method.php

With no directory URL 2: example.com/class/method Template::load() will find the pages on views/pages/class/method.php



  1. Add This file on the libraries folder
  2. Load the file using $this->load->libraries('template') or add on the autoload.php config
  3. Setup the template_path and the pages_path (must be inside the views folder)
  4. You are good to go!!


load our main template

Template::set('template','temp_main')

//minify data upon output

//removes whitespaces and tab indents

//default TRUE

Template::set('minify',true);

compress data upon output gzip deflate default TRUE Template::set('compress',true);

load CSS Dynamically

_add ^ on the beginning so that Template will ignore and not add a base URL

Template::set('css',[ 
 'test/test/test',
 '^http://www.google.com', 			#add ^ on the first part for external links
 '^http://www.testing.com?getid=12?4434',	#add ^ on the first part for external links
 '^cxds.com',				#add ^ on the first part for external links
 '^path/test/123',				#add ^ on the first part for external links
]);`

load javascript links default javascript location at footer

add ^ on the beginning so that Template will ignore and not add a base URL

Template::set('javascript',[
 'test/test/test',
 '^http://www.google.com', 		 #add ^ on the first part for external links
 '^http://www.testing.com?getid=12?4434', #add ^ on the first part for external links
 '^cxds.com',				 #add ^ on the first part for external links
 '^path/test/123',			 #add ^ on the first part for external links
]);

javascript for the head

Template::set('javascript',['test/test/test'],'head');

partials

Template::set('partials',[
 'spam_page' => 'foo/bar/spam',
 'eggs_page' => 'foo/bar/eggs',
]);

data for output to views

automatically sends the data to the partials also

 Template::set('data',[
  'data1' => 'value data 1',
  'data2' => 'value data 2',
  'data3' => 'value data 3',
  'data4' => ['eggs','orange','apples'],
]);

auto load the page

will look for the VIEWS/DIR/CLASS/METHOD.php or VIEWS/CLASS/METHOD.php

Template::load();

mANUAL LOAD A SPECIFIC FILE

Template::load('pages/for/not/tony');

Variables to use on the Template Page:

  • Load Javascript on the header = $TEMPLATE_JS_HEAD

  • Anonymous functions to access the partials data = $TEMPLATE_PARTIALS('nonexistent',['name'=>'Jane Doe']);

  • Loads the Body of the page = $TEMPLATE_CONTENT

  • Loads the Footer javascript files = $TEMPLATE_JS_FOOT

  • Loads the CSS files = $TEMPLATE_CSS

Redirect to a Page with Flashdata

Template::redirect('path/to/redirect','Message to be saved','key');

Automatically available in views data as a string with variable name $_FLASHDATA_key

Template::redirect('path/to/redirect',array('test','tost'),'keyofFlash');

Automatically available in views data as an array with variable name $_FLASHDATA_keyofFlash


Sample template page

<!DOCTYPE html>
<html>
<head>
	<title>Test</title>
	<?php echo $_TEMPLATE_CSS_; ?>	
	<?php echo $_TEMPLATE_JS_HEAD_; ?>	
</head>
<body>
	<section>
		<?php 
			//this partial is not initialized
			echo $_TEMPLATE_PARTIALS_('nonexistent',['name'=>'Jane Doe']);
		?>
	</section>
	<div>
		<?php echo $_TEMPLATE_CONTENT_; ?>
		<?php echo $_TEMPLATE_PARTIALS_('spam_page',['name'=>'John Doe']); ?>
	</div>
	<?php echo $_TEMPLATE_JS_FOOT_; ?>
</body>