Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by Agung Wahyudiono )

Template Parser for CI 2.0

This is a library that extends the CI Template Parser Library. It’s use to manage the templates file so its could grouped into specific folders. The templates file must implement some rule, like the other CMS use.

This library use the template parser characteristic , on using delimiter.
This is some base rule to use this library

1. Template File use to have 3 main file, page.php, header.php, footer.php
2. This library use {} delimiter, and there was several reserved tag such as {body},{judul},{metadata}
3. For Parsial template, the tag use to mention on page.php and its tag is the same name with the file name, eg: sidebar.php, the tag name is {sidebar.php}
4. to parse data into parsial template, use the tag that have rules filename followed by “_data” . eg:: for sidebar.php use {sidebar_data} in it
[/list] 

To see more clearly just read the source code

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Template extends CI_Parser {
    
private $ci;
    private 
$name;
    private 
$path;
    private 
$base;
    private 
$default 'default';
    private 
$css_folder 'css';
    private 
$js_folder 'js';
    private 
$delimiter = array();
    private 
$metadata = array('judul'=>'title');
    private 
$parsial = array();
    private 
$prepdata = array();
    
    public function 
__construct(){
        $this
->ci =& get_instance();
        
$this->path $this->ci->config->item('template_base');
        
$this->base $this->path.$this->default.'/';
        
$this->metadata['metadata''';
    
}
    
    
public function set_template($name 'default'{
        $this
->base $this->path.$name.'/';
        
$this->name $name;
        return 
$this;
    
}
    
    
public function set_judul($judul 'title'{
        $this
->metadata['judul'$judul;
        return 
$this;
    
}
    
    
public function set_css($folder='',$file ''){
        
if(func_num_args() < 2{
            $folder 
'';
            
$file func_get_arg(0);
        
else {
            $folder 
$folder.'/';
        
}
        
        $pre 
'<link href="';
        
$pre .= base_url().$this->base.$this->css_folder.'/'.$folder;
        
$end '" rel="stylesheet" >';
        
        
$this->_delimiter($pre,$end);
        
$this->_set_asset($file,'css');
        
        return 
$this;
    
}
    
    
public function set_js($folder '',$file ''{
        
if(func_num_args() < 2{
            $folder 
'';
            
$file func_get_arg(0);
        
else {
            $folder 
$folder.'/';
        
}
        
        $pre 
'[removed]ci->base_url().$this->base.$this->js_folder.'/'.$folder;
        $end = '">[removed]';
        
        
$this->_delimiter($pre,$end);
        
$this->_set_asset($file,'js');
        
        return 
$this;
    }
    
    public function set_parsial (
$location = '', $view = '', $data = '') {
        if(
$location != '' && $data != '') {
            
$this->parsial[$location] = array ($location.'_data'=>$this->ci->load->view($view,$data,TRUE)); 
        }
        return 
$this;
    }
    
    //DELETEABLE
    public function view_config(){
        echo 
$this->base;
    }
    
    private function _cek_file(){
        if(!file_exists(
$this->base)){
            show_error('File template dengan nama "
<b>'.$this->name.'</b>" tidak dapat ditemukan');
        }
    }
    
    public function render(
$view$data$state = FALSE){
        
$this->_cek_file();
        
$this->prepdata['body'] = $this->ci->load->view($view,$data,TRUE) ;
        
        
$this->ci->load->_ci_view_path = $this->base;
        
        foreach (
$this->parsial as $key => $val) {
            
$this->prepdata[$key] = parent::parse($key,$val,TRUE);
        }
        
        //print_r(
$this->parsial);
        parent::parse('header',
$this->metadata,FALSE);
        parent::parse('page', 
$this->prepdata, FALSE);
        parent::parse('footer', 
$data, FALSE);
    }
    
    private function _set_asset(
$file,$ext) {
        if(is_array(
$file)) {
            
$hasil = array();
            foreach(
$file as $key => $filename) {
                
$hasil = $this->delimiter['awal'];
                
$hasil = $filename.'.'.$ext;
                
$hasil = $this->delimiter['akhir'];
            } 
        } else {
            
$hasil = $this->delimiter['awal'].$file.'.'.$ext.$this->delimiter['akhir'];
        }
        
        
$this->metadata['metadata'] .= $hasil;
    }
    
    private function _delimiter(
$awal='',$akhir=''){
        
$this->delimiter['awal']=$awal;
        
$this->delimiter['akhir']=$akhir;
    }
    
}

?> 

End then, to use this library this is the code u have to write out

$this->template->set_judul('My E-Commerce')
        ->
set_css('style')
        ->
set_parsial('template_file','view_file',$data)
        ->
set_parsial('topmenu','top_view',$data)
        ->
render('store',$data); 

This code is not 100% tested. so I’ll make a change if this library have a bug.