Part of the EllisLab Network
   
2 of 3
2
Modulated Templated CMS system
Posted: 16 March 2008 09:02 PM   [ Ignore ]   [ # 11 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  09-07-2007

I apologize for not explaining everything from the beginning. This is what I have done in one of my configuration tests with ME & smarty Template System. I will start with the autoload file, your routes.php file will stay as above. Ofcourse you can do some trickiness it there to load another controller(I don’t see a need for this due to modules) When you use ME it this configuration everythign becomes a module that is loaded from the url. I check my method or current_action inside my module and the only thing on top is the error flash messages or contact(email send function),etc.which is mapped from the index function via function_exists. Please try to follow along as I give you some files for example.

autoload.php

$autoload['libraries'] = array('database','session','validation','smarty_parser','environ');

$autoload['helper'] = array('url','file','form','modules');

$autoload['language'] = array('site'); //app/languages/english/site_lang.php

make sure to add smarty libs to your app/libraries/smarty/ directory, create a app/libraries/smarty_parser.php file and app/config/smarty_parser.php(follow 1st post link) In the research that I have conducted I have found that all you need is a default_layout.php that contains this code

<?=$template;?>

and teh rest is up to smarty and my special site environment variable library. I will show a basic configuration of this and my lib to hold your environment configuration.
inside my smarty I have added these to setting to control my template delimiters (this way anything smarty will show as an html comment and you can tell you designer not to touch the comments but work around them, plus it looks easier on the eyes smile

$config['left_delimiter'] = '<!--{';
$config['right_delimiter'] = '}-->';

okay, this is where the fun begins, yay, fun. smile
create a environ.php inside app/config/ this will hold your template configuration

//templates folder name in filesystem
$config['templates_folder']='templates/';
//current active template name
$config['template_name']="CLIENT_1";
//template config file name located inside template_name/config/ directory
$config['theme_config_file']='theme';

I will continue with app/libraries/environ.php in next post, I thank you for being patient. I’m sure you will be pleased with it.

 Signature 

smarty-hmvc-enviroment-variables

Profile
 
 
Posted: 17 March 2008 04:36 AM   [ Ignore ]   [ # 12 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  09-07-2007

This is my current eviron.php file located inside app/libraries/ I will place it on the wiki once it deserves to be up there and is nicely working.

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

ENVIRON VARIABLES LIBRARY for ME & Smarty Template System
by
Aleksandr Ilyich Goretoy aka RussianPimp YingYes

This class is very early beta and will be rigourously tested and modified
You are allowed to use this code as you wish as long as this sig stays here. Please don't delete it.
Currently works for loading templates from file system and db(kinda buggy) Send me bugfixes in ci forum.
*/
class Environ {
    
var $template_name;
    var
$templates_folder;
    var
$template;
    var
$template_root;
    var
$template_dir;
    var
$_environ=array();
    var
$theme=array();
    var
$data=array();
    function
__construct()
    
{
        $this
->ci =& get_instance();
        
$this->ci->load->helper('url');
        
define('ENVIRONMENT_VERSION', '1.0.4');
        
log_message('debug', "Environment Class Initialized : Environ " . ENVIRONMENT_VERSION);
        
$this->init();

        

    
}
    
function init($environ=array())
    
{    
        
// Load the required files for this lib to
        // work correctly.
        
$this->ci->load->model('environ_model');
        
$this->ci->load->model('template_model');
        
$this->ci->config->load('environ');
        
        
//get environment variables from config file
        
$this->document_path = $_SERVER['DOCUMENT_ROOT'].$this->ci->config->item('base_dir').APPPATH.'views/';
        
$this->template_name = $this->ci->config->item('template_name');
        
$this->templates_folder=$this->ci->config->item('templates_folder');
        
$this->theme_config_file=$this->ci->config->item('theme_config_file');
        
$this->template_location = $this->ci->config->item('template_location');
        
$this->_set_template();
        if(
strtolower($this->template_location)=='file')
        
{
            
/*this line loads the config file located inside the template directory
            app/views/smarty/templates/$template_name/config/$theme_config_file
            */
            
$this->ci->load->config('../views/'.$this->template.'config/'.$this->theme_config_file,true,true);
            
$this->_theme_config($this->ci->config->item('../views/'.$this->template.'config/'.$this->theme_config_file));
            
        
}
        
else
        
{
            $this
->template_id = $this->ci->template_model->get_config_item_value('template_id');
            
$this->template_name = $this->ci->template_model->get_config_item_value('template_name');
        
}
        $this
->_template_asset_dirs($this->template_location);
        
        
$this->_template_theme('css',$this->template_location);
        
$this->_template_theme('js',$this->template_location);
    
        
$this->_template($this->template_location,'layout');
        
$this->_templify_array($this->data['template_layout'],'layout');
        
        
$this->_template($this->template_location,'admin');
        
$this->_templify_array($this->data['template_admin'],'admin');    
                
        
$this->_template($this->template_location,'pages');                  
        
$this->_templify_array($this->data['template_pages'],'pages');
    
}
    
function _templify_array($array,$string='layout')
    
{
        
if(is_array($array))
        
{
            
foreach($array as $key=>$val)
            
{
                $path
=preg_split('/\//',$val);
                if(
$val!='index')
                
{
                    
if(isset($path[1]))
                    
{
                        
if(file_exists($this->template_root.$path[0].'/tpl_'.$path[1].EXT))
                        if(
$this->ci->smarty_parser->template_exists('ci:'.$this->template.$path[0].'/tpl_'.$path[1].EXT))
                        
{
                        $this
->data['template_'.$string][$path[0].'_'.$path[1].'_tpl']='ci:'.$this->template.$path[0].'/tpl_'.$path[1].EXT;
                        
}
                    
                        
if(file_exists($this->template_root.$string.'/tpl_'.$val.EXT))
                        if(
$this->ci->smarty_parser->template_exists('ci:'.$this->template.$string.'/tpl_'.$val.EXT))
                        
{
                            $this
->data['template_'.$string][$val.'_tpl']='ci:'.$this->template.$string.'/tpl_'.$val.EXT;
                        
}
                        
                    }
                    
else
                    
{
                        
if(file_exists($this->template_root.'tpl_'.$val.EXT))
                        if(
$this->ci->smarty_parser->template_exists('ci:'.$this->template.'tpl_'.$val.EXT))
                        
{
                            $this
->data['template_'.$string][$val.'_tpl']='ci:'.$this->template.'tpl_'.$val.EXT;
                        
}
                        
                        
if(file_exists($this->template_root.$string.'/tpl_'.$val.EXT))
                        if(
$this->ci->smarty_parser->template_exists('ci:'.$this->template.$string.'/tpl_'.$val.EXT))
                        
{
                            $this
->data['template_'.$string][$val.'_tpl']='ci:'.$this->template.$string.'/tpl_'.$val.EXT;
                        
}
                        
                    }
                    
                }
            }
            
        }
    }

cut and paste these 3 sections.

 Signature 

smarty-hmvc-enviroment-variables

Profile
 
 
Posted: 17 March 2008 04:38 AM   [ Ignore ]   [ # 13 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  09-07-2007

environ.php continued:

function _template_asset_dirs($loc='file')
    
{
        
if($loc=='db')
        
{
            $assets
=$this->ci->template_model->get_asset_dirs_by_id($this->template_id);
            foreach(
$assets->result() as $asset=>$a)
            
{
                $this
->data['template_images_dir']=$this->template_dir.$a->images_dir;
                
$this->data['template_swf_dir']=$this->template_dir.$a->swf_dir;
                
$this->data['template_pdf_dir']=$this->template_dir.$a->pdf_dir;
            
}        
        }
        
else
        
{
            $this
->data['template_images_dir']=$this->template_dir.$this->data['template_images_dir'][0];
            
$this->data['template_swf_dir']=$this->template_dir.$this->data['template_swf_dir'][0];
            
$this->data['template_pdf_dir']=$this->template_dir.$this->data['template_pdf_dir'][0];            
        
}

    }
    
//type can be either css or js and loc can be db or file
    
function _template_theme($type='css',$loc='file')
    
{
        $func
='link'.strtoupper($type);
        if(
$loc=='db')
        
{            
            $vart
='theme_'.$type;
            
$varg='get_'.$vart;
            $
$vart=$this->ci->template_model->$varg($this->template_id);
            foreach($
$vart->result() as $$type=>$c)
            
{
                $this
->data['theme_'.$type]=$this->$func($this->template_dir.$c->url,'theme_'.$type,true);
            
}
        }
        
else
        
{
            
foreach($this->data['theme_'.$type] as $$type=>$c)
            
{    
                $this
->data['theme_'.$type]=$this->$func($this->template_dir.$c,'theme_'.$type,true);
            
}    
        }
    }
    
function _template($loc='file',$string='layout',$type='id')
    
{
        
if($loc=='db')
        
{
            $func
='get_'.$string.'_by_'.$type;
            
$vart='template_'.$type;
            $
$string=$this->ci->template_model->$func($this->$vart);
            
print_r($$string->result());    
            foreach($
$string->result() as $k=>$v)
            
{
                
if($string=='pages')
                if(
$v->status=='front_page')
                
{
//                    if(file_exists($this->template_root.$string.'/tpl_'.$p->url.EXT))
//                    if($this->ci->smarty_parser->template_exists('ci:'.$this->template.$string.'/tpl_'.$p->url.EXT))
//                    {
                        
$this->data['template_'.$string]['index_tpl']=$v->text;//'ci:'.$this->template.$string.'/tpl_'.$p->url.EXT;
//                    }
                
}
                $this
->data['template_'.$string][$v->url.'_tpl']=$v->text;
            
}
        }
        
else
        
{
            
foreach($this->data['template_'.$string] as $k=>$v)
            
{
                
if(file_exists($this->template_root.$string.'/tpl_'.$v.EXT))
                if(
$this->ci->smarty_parser->template_exists('ci:'.$this->template.$string.'/tpl_'.$v.EXT))
                
{
                    $this
->data['template_'.$string][$this->data['template_'.$string][$k].'_tpl']='ci:'.$this->template.$string.'/tpl_'.$v.EXT;
                
}    
            }

        }

    }



    
//this function sets the template directory variable variants
    
function _set_template()
    
{
        
if($this->template_name=='')
        
{
            $this
->template_name='default_template';
            
$this->template='smarty/'.$this->template_name.'/';
            
$this->template_root=$this->document_path.'smarty/'.$this->templates_folder.$this->template_name.'/';
            
$this->template_dir=$this->template.'/';
        
}
        
else
        
{
            $this
->template='smarty/'.$this->templates_folder.$this->template_name.'/';
            
$this->template_root=$this->document_path.'smarty/'.$this->templates_folder.$this->template_name.'/';
            
$this->template_dir=site_url().$this->templates_folder.$this->template_name.'/';
        
}
    }
    
//when configuration is from flat file this function sets the template variables into data
    
function _theme_config($theme_config='')
    
{
        
if(is_array($theme_config))
        
{
            
foreach($theme_config as $the =>$theme)
            
{
                
if(is_array($theme))
                
{
                    
foreach($theme as $var =>$val)
                    
{
                        $this
->data[$val]=$the;
                    
}
                }
                
else
                
{
                    $this
->data[$theme]=$the;
                
}
            }
        }
        
        $this
->data=$this->_array_invert($this->data);
    
}
    
//when config is from flat file this function inverts the data array
    
function _array_invert($arr)
    
{
        $res
= Array();
        foreach(
array_keys($arr) as $key)
      
{
          
if (!array_key_exists($arr[$key], $res)) $res[$arr[$key]] = Array();
        
array_push($res[$arr[$key]], $key);
      
}
      
return $res;
    
}

 Signature 

smarty-hmvc-enviroment-variables

Profile
 
 
Posted: 17 March 2008 04:39 AM   [ Ignore ]   [ # 14 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  09-07-2007

environ.php continued:

//these 2 functions linkCSS and linkJS were borrowed from View Library
/**
*    View Library - simplifies management and loading of views in CodeIgniter applications
*
*    @author            Ted Wood (ted[at]codeoflife.ca)
*    @last_modified    July 28th, 2007
*
*/
function linkCSS($href, $var = 'view_css',$return=false)
    
{
        
if (!isset($this->vars[$var])) {
            $this
->vars[$var] = "<!-- @{$var} -->\n\t";
        
}
        
if (is_array($href)) {
            
foreach ($href as $v) {
                $this
->vars[$var] .= '<link type="text/css" rel="stylesheet" href="'.$v.'" />'."\n\t";
            
}
        }
else {
            $this
->vars[$var] .= '<link type="text/css" rel="stylesheet" href="'.$href.'" />'."\n\t";
        
}
        
if($return)
        return
$this->vars[$var];
    
}
    
function linkJS($src, $var = 'view_js',$return=false) {
        
if (!isset($this->vars[$var])) {
            $this
->vars[$var] = "<!-- @{$var} -->\n\t";
        
}
        
if (is_array($src)) {
            
foreach ($src as $v) {
                $this
->vars[$var] .= ''."\n\t";
            
}
        }
else {
            $this
->vars[$var] .= ''."\n\t";
        
}
        
if($return)
        return
$this->vars[$var];
    
}
}

environ.php end/close/finish/fin smile
next I will show how to use this powerful setup.

 Signature 

smarty-hmvc-enviroment-variables

Profile
 
 
Posted: 17 March 2008 04:52 AM   [ Ignore ]   [ # 15 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  209
Joined  09-07-2007

this is my templates_model.php located inside app/models/

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package        CodeIgniter
* @author        ExpressionEngine Dev Team
* @copyright    Copyright (c) 2006, EllisLab, Inc.
* @license        http://codeigniter.com/user_guide/license.html
* @link        http://codeigniter.com
* @since        Version 1.0
* @filesource
*/

// ------------------------------------------------------------------------

/**
* CodeIgniter User Authentication Model
*
* This Model provides CodeIgniter with a simple
* and flexible user authnetication
*
* @package        CodeIgniter
* @subpackage    Model
* @category    Model
* @author        Mathew Davies
* @link        
*/

class Template_Model extends Model
{
    
var $query_count=0;
    function
__construct()
    
{
        parent
::Model();
    
}
    
function clear_count()
    
{
        $this
->query_count=0;
    
}
    
function get_count()
    
{
        
return $this->query_count;
    
}
    
function get_pages_by_id($template_id)
    
{
        
if(!empty($template_id))
        
{    
            $this
->query_count++;
            return
$this->db->query("select * from `pages` where `template`='".$template_id."'");
        
}
        
return false;
    
}
    
function get_pages_by_name($template_name)
    
{
        
if(!empty($template_name))
        
{    
            $template_id
= $this->get_template_id_by_name($template_name);
            if(
$template_id!=false)
            
{
                $this
->query_count++;
                return
$this->db->query("select * from `pages` where `template`='".$template_id."'");
            
}
            
else
            
{
                
return false;
            
}
        }
        
return false;
    
}
    
function get_layout_by_id($template_id)
    
{
        
if(!empty($template_id))
        
{    
            $this
->query_count++;
            return
$this->db->query("select * from `layout` where `template`='".$template_id."'");
        
}
        
return false;
    
}
    
function get_layout_by_name($template_name)
    
{
        
if(!empty($template_name))
        
{    
            $template_id
= $this->get_template_id_by_name($template_name);
            if(
$template_id!=false)
            
{
                $this
->query_count++;
                return
$this->db->query("select * from `layout` where `template`='".$template_id."'");
            
}
            
else
            
{
                
return false;
            
}
        }
        
return false;
    
}
    
function get_sections_by_id($template_id)
    
{
        
if(!empty($template_id))
        
{    
            $this
->query_count++;
            return
$this->db->query("select * from `sections` where `template`='".$template_id."'");
        
}
        
return false;
    
}
    
function get_theme_css($template_id)
    
{
        
if(!empty($template_id))
        
{    
            $this
->query_count++;
            return
$this->db->query("select * from `css` where `template`='".$template_id."'");
        
}
        
return false;
    
}
    
function get_theme_js($template_id)
    
{
        
if(!empty($template_id))
        
{    
            $this
->query_count++;
            return
$this->db->query("select * from `js` where `template`='".$template_id."'");
        
}
        
return false;
    
}
    
function get_asset_dirs_by_id($template_id)
    
{
        
if(!empty($template_id))
        
{    
            $this
->query_count++;
            return
$this->db->query("select * from `templates` where `id`='".$template_id."'");
        
}
        
return false;
    
}
    
function get_asset_dirs_by_name($template_name)
    
{
        
if(!empty($template_name))
        
{    
            $this
->query_count++;
            return
$this->db->query("select * from `templates` where `name`='".$template_name."'");
        
}
        
return false;
    
}
    
function left_join_by_template_id($t_id)