[eluser]a&w[/eluser]
Thanks. I'm getting a better picture now. Thanks for elaborating so well. Please note, that I think in Post#2 your directory structure is different, hence my seeking clarification on the structure. Your 'home' folder is not inside 'modules'.
I had not even considered nesting the modules folder, where you've shown it being a child folder. Interesting. Not sure when it would ever come to practice, but good to know.
What is the difference between modules::load and modules::run? A cursory look at the code looks like run also loads the module passed.
With reference to the line below, do you check if data['current_action'] isset in your index method of the controller then?
Code: $this->data['current_action']=$method; //used inside my modules
This next part is probably application specific, nonetheless, anything interesting to note with the following snippet?
Code: $content = ($module=='admin')?modules::run($module, $this->data, $method):modules::run($module, $this->data, 'index');
}
else
{
redirect('');
}
I didn't quite follow you when you say you use a custom library to load all the options inside the data array upon initialization and then just array_merge. I'm a bit rusty on CI (took a sabatical for a few months learning javascript), but I thought when libraries were loaded, CI automatically checked for a similarly named config file. Seems like there would be a way befitting of the "CI way" to have the modules check for a config file and to load that. So when a module is loaded, go ahead and load the config array associated with it. Not sure if that would connect to the module, or maybe one of the views.
[eluser]Avatar[/eluser]
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
Code: $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
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
Code: $config['left_delimiter'] = '<!--{';
$config['right_delimiter'] = '}-->';
okay, this is where the fun begins, yay, fun.
create a environ.php inside app/config/ this will hold your template configuration
Code: //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.
[eluser]Avatar[/eluser]
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.
Code: <?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.
[eluser]Avatar[/eluser]
environ.php continued:
Code: 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;
}
[eluser]Avatar[/eluser]
environ.php continued:
Code: //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
next I will show how to use this powerful setup.
[eluser]Avatar[/eluser]
this is my templates_model.php located inside app/models/
Code: <?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://ellislab.com/codeigniter/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)
{
if(!empty($name))
{
$this->query_count++;
$q=$this->db->query("SELECT templates.name, templates.images_dir, templates.swf_dir, templates.pdf_dir,
layout.name,layout.url
FROM templates
LEFT JOIN layout
ON templates.id=layout.template_id
WHERE templates.id='".$t_id."'");
return $q->row()->id;
}
return false;
}
function get_config_item_value($item_name)
{
if(!empty($item_name))
{
$this->query_count++;
$q=$this->db->query("select `value` from `site_config` where `name`='".$item_name."'");
return $q->row()->value;
}
return false;
}
function get_template_id_by_name($name)
{
if(!empty($name))
{
$this->query_count++;
$q=$this->db->query("select `id` from `templates` where `name`='".$name."'");
return ($q->num_rows()!=0)?$q->row()->id:false;
}
return false;
}
function set($data, $name = '' )
{
if ( empty($name) && is_array($data) )
{
$this->db->set($data);
}
else
{
$this->db->set($name, $data);
}
}
function insert ($table)
{
$this->db->insert($table);
if ( $this->db->affected_rows() > 0 )
{
return true;
}
return false;
}
}
[eluser]Avatar[/eluser]
this is where the configuration of modules and such begins. everything above this is environ lib.
this is how I have my file system structure setup for one template:
Code: app/
.htaccess //< Options -indexes RewriteEngine On Deny from all
config/
environ.php
smarty_parser.php
controllers/
default_controller.php
helpers/
modules_helper.php
languages/
english/
site_lang.php // autoloaded
russian/
site_lang.php
libraries/
environ.php
modular_extensions.php
smarty_parser.php
models/
template_model.php
modules/
home/
controllers/
home.php
views/ // we won't use module views since all our views are smarty templates and will be inside app/views/smarty/templates
views/
default_layout.php
smarty/
default_template/ //this is your default template which loads if no var is set in file or db
templates/
CLIENT_1/
admin/
tpl_account.php
tpl_pages.php
tpl_templates.php
assets/
.htaccess //< Options -indexes RewriteEngine On Allow from all
css/
images/
js/
pdf/
swf/
config/
theme.php
pages/
tpl_about.php
tpl_contact.php
tpl_page.php
tpl_admin.php
tpl_head.php
tpl_foot.php
tpl_left.php
tpl_content.php
tpl_index.php //this loads your head foot left right and any other templates
CLIENT_2/
/* RECURSION */
configure the main smarty template which loads all the rest of the templates from variables
this is what your tpl_index.php should looks like
Code: <!--{if !empty($head_tpl)}-->
<!--{include file="$head_tpl"}-->
<!--{/if}-->
<!--{if !empty($banner_tpl)}-->
<!--{include file="$banner_tpl"}-->
<!--{/if}-->
<div class="mainWrap">
<!--{if !empty($left_tpl)}-->
<!--{include file="$left_tpl"}-->
<!--{/if}-->
<!--{if !empty($content_tpl)}-->
<!--{include file="$content_tpl"}-->
<!--{/if}-->
<!--{if !empty($right_tpl)}-->
<!--{include file="$right_tpl"}-->
<!--{/if}-->
</div>
<!--{if !empty($foot_tpl)}-->
<!--{include file="$foot_tpl"}-->
<!--{/if}-->
tpl_content.php
Code: <div class="centerWrap">
<!--{$response}-->
<!--{foreach from=$template_pages key=template item=template_page}-->
<!--{if !empty($template_page)}-->
<!--{if $current_action eq $template}-->
<!--{include file="$template_page"}-->
<!--{/if}-->
<!--{/if}-->
<!--{/foreach}-->
<!--{if !empty($page_tpl)}-->
<!--{if $current_action eq 'index'}-->
<!--{include file="$page_tpl"}-->
<!--{/if}-->
<!--{/if}-->
</div>
[eluser]Avatar[/eluser]
on top of all this great commosion you can create your own custom smarty functions inside app/libraries/smarty/plugins/
oooooohhh, pluuuuuuugiiiiiinnnns. very nice touch.
app/libraries/smarty/plugins/function.site_url.php
Code: <?php
function smarty_function_site_url($params,&$smarty)
{
//check if the needed function exists
//otherwise try to load it
if (!function_exists('site_url')) {
//return error message in case we can't get CI instance
if (!function_exists('get_instance')) return "Can't get CI instance";
$CI= &get;_instance();
$CI->load->helper('url');
}
if (!isset($params['url'])) return base_url();
else return site_url($params['url']);
}
?>
[eluser]Avatar[/eluser]
tpl_head.php
Code: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title><!--{$page_title}--></title>
<meta http-equiv="Content-Type" content="text/html; charset=<!--{$charset}-->">
<meta name="keywords" content="<!--{$keywords}-->">
<meta name="description" content="<!--{$description}-->">
//preload images for lightbox.js
var fileLoadingImage = "<!--{$template_images_dir}-->loading.gif";
var fileBottomNavCloseImage = "<!--{$template_images_dir}-->closelabel.gif";
<!--{foreach from=$theme_css item=theme}--><!--{$theme}--><!--{/foreach}-->
<!--{foreach from=$theme_js item=theme}--><!--{$theme}--><!--{/foreach}-->
</head>
<body>
I would appreciate any comments at all and whatsoever a nice job would be nice too. Anyway, I hope that someone takes this lib very seriously as it has a lot of power due to the fact that it uses ModularExtensions and Smarty combined. Any and all input is welcome as always.
[eluser]Avatar[/eluser]
and this is a module for ya as well.
home.php located inside app/modules/home/controllers/home.php
Code: function index($_data)
{
if($_data['current_action']=='contact' && method_exists($this,'contact'))
{
$this->contact($_data);
}
//$this->sibling_content=($this->sibling!='')?modules::run(get_class($this).'/'.$this->sibling.'/'.$this->sibling,$_data,$this->sibling_method):'';
$data = array(
'current_page'=>$_data['current_page'],
'current_action'=>$_data['current_action'],
'title'=>get_class($this).' - '.$this->config->item('site_name'),
'page_title'=>'Tulsa / Broken Arrow - Oklahoma Interior Design, Renovation Services | Elegant Accents '
);
$data=array_merge($data,$_data);
return $this->render('ci:'.$this->environ->template.'tpl_index',$data);
}
as you can see I'm not loading a mass load of modules yet my pages have so much data on them and all that good stuff
Do you Google much?
I Google much alot.
|