Codeigniter DOM based template library
-
Your template file
./application/views/template/default.php
<!DOCTYPE html> <html> <head> <title><?php echo content('title','Untitled') ; ?></title> </head> <body> <?php echo content('body', 'No Content') ; ?> <hr> <?php echo content('footer', 'If footer block is not provided, this is the default footer text') ; ?> </body> </html>
-
Your view file
./application/views/myview.php
<template block="title"> Welcome to Simple Codeigniter Template </template> <template block="body"> This is the <i>body</i> section <!-- some html comment --> </template>
-
Your controller file
./application/controller/Welcome.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Welcome extends CI_Controller { public function index() { // $this->load->library('template'); // remark this line if you have autoloaded the library // $this->load->helper('template'); // remark this line if you have autoloaded the helper // sample data to be loaded into view just like using $this->load->view('View File', $data); $data['some_text'] = "Some content text here"; // load views/myview.php with $data, rendered into 'default' template located at views/template/default.php $this->template->render('myview', $data, 'default') ; } }
- Simply copy the following files into your codeigniter project
./application/libraries/Template.php
./application/helpers/template_helper.php
- Create a new folder called
template
under./application/views/
and put your templates in that folder - (Recomended) Autoload the Template library and template helper from your
./application/config/autoload.php
file - Start using the template library in your controller like this
$this->template->render('myview',$mydata,'template_file') ;
- Avoid new file extension such as .tpl
- Follow inheritance templating method
- Use of built in PHP method in parsing template blocks
- Suitable for beginner and cater to simple use cases
- Create a testing template to measure the performance of the library