Welcome Guest, Not a member yet? Register   Sign In
Smarty HMVC Environment Variables 1.0.5
#1

[eluser]Avatar[/eluser]
First of all I want to thank all people from helping me make this happen. I want o give it to the community to just see where it goes. I know youguys/girls can put a twist on it a may not be able to, and that is just interesting to me to find out where this lib will go. Just like any other CI lib. My library uses other libraries in contrastto make a complete(almost) templated modulated cms system with great easy. When your setup is complete you will be able to drop in a new module or a view inside the smarty template dir or if not using smarty you can modify code. I won't do this for you and I would ask not to please ask because this threads main focus is on smarty and modular extensions in unison to make a complete solution for dynamic views and templated modules. when you have this setup that I will explain, I would suggest to only store your templates in the templated schema outlined for the library to work and not use any views(although you can, but why would you want to when smarty is here)with smarty displaying variables data is a snap or even doing a foreach loop. Plus, in the smarty_parser.php inside config dir I've set it to modify the right and left delimiter to make smarty code look like html comments. This is great, you know why? Because then if you have a developer that knows only good development and not such good design. Let him do it and if you have on the other hand a good designer who doesn't know code he can just know to work around the html comment. Plus everything is dynamically loaded into templates from auto loading environ. It loads all the variables on every page load(I will fix this so it doesn't load all pages but only needed ones later and also super size functions later as well) the in side your default controller it mergers the array into the array that gets passed to the modules function/method(for the most part if you create a new variable inside the config you can use it inside your template just like that, in my tests it has shown this. haven't checked lately.) I know for sure I can minimize my sql queries to be less by super sizing them. I will get to this later, newer versions. Basically where the meat of the lib control lies is in the app/config/environ.php file. this is where it determines where it will get the rest of the configuration from. The database(doesn't load config file from filesystem inside templates/template_name/config/) or the filesystem(inside templates/template_name/config/) the reason why I have done this is when you have your templates config stored inside db you can modify it easier from admin login and then make the modifications reflect on the filesystem where the template file is located. Maybe something drag-and-drop with JQuery. Anyway, The rest I'm sure ya'll can figure out . I hope this helps someone learn CI. You can use this lib for commercial use or personal use as long as my header stays inside the files.

you can get it here

Please let me know of any bugs, would be greatly appreciated.
#2

[eluser]PedroGrilo[/eluser]
Nice work.
PHP5 only, right?

EDIT:nvm. I just updated to PHP5. Can't stand being left in the cold... Wink
#3

[eluser]Avatar[/eluser]
Thank you. Yes, you are correct. I also only do PHP5, as soon as my hosting provider installs mySQL 5 I'll be able to use Stored Procedures. I get all giggly just thinking about it. Smile
#4

[eluser]Avatar[/eluser]
This a a basic module method that extends the module with this setup. In your default_controller.php do this and pass to the module:
Code:
$this->data['segment_array']=$this->uri->segment_array();
now for teh extended module method:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Blog home method
*
**/
class Templates extends Admin
{
    function Templates()
    {
        parent::Admin();
    }
    
    function index()
    {        
        $_data=func_get_args();
        $_data=array_merge($_data,$_data[0]);
        if(isset($_data['segment_array'][3]))
        {
            if(method_exists($this,'_'.$_data['segment_array'][3]))
            {
                return call_user_func_array(array($this,'_'.$_data['segment_array'][3]),$_data);
            }
            else
            {    //method doesn't exist so we redirect to admin
                redirect('admin');
            }
        }
        return call_user_func_array(array($this,'_index'),$_data);
    }
    function _index($_data)
    {
        if($_data['logged_in']!=$this->config->item('logged_in_value'))
        {
            redirect('admin');
        }
        else
        {
            $data = array(
                'current_action'=>strtolower(get_parent_class($this)).'_'.strtolower(__CLASS__).'_'.__FUNCTION__.'_tpl'
            );
            
            $data=array_merge($_data,$data);
            return $this->render($data);
        }
    }
}
This will effectively map your function calls from the url, if method doesn't exist just redirect to admin module. Hope this helps. version 1.0.6 will come out soon and I will give a better documentation and theres a few more added features to modify pages, sections and templates from admin login with tinymce. I plan on making this a easy this to implement out of the box. Also if there will be an updateyou won't have to update more than two files(config/environ and libs/environ). How it works is the templates are stored in the filesystem but the name url and backup for the template are store in the database. I will be adding alot more features as I think of them. I would greatly appreciate some comments as to what features to implement though. Currently the current_action is what determines what template should display in the content, yet it still loads all pages. I would like to make it load only what it needs. Currently what I have setup if I use the db option it only runs 11 queries if I use the file option it uses files for all configuration options and loads them with no queries to db at all. I may remove this feature and just have db and then file_get_contents() on templates. Plus I want to do file locking, I have an idea of how I would implement this as well and will do so soon. Any ideas on this would be good too. Also, I will be adding sections. so that a user can add sections to the pages, this will work via smarty plugin and you will be able to do something like this:
Code:
//smarty code inside template
<!--{page_section name="top_ads"}-->
also I came up with some trickiness for navigation loading(this will be in next version), inside your template:
Code:
<div class="leftWrap">
                                    
                    <div class="navWrap">
                    
                        <div class="topNav"><img src="&lt;!--{$template_images_dir}--&gt;navi.jpg"></div>
                        
                        <div class="sectionsNav">
                        <div class="homeNav"><a href="&lt;!--{site_url}--&gt;">Home</a></div>
                        &lt;!--{if !empty($navigation)}--&gt;
                        &lt;!--{foreach from=$navigation key=navi item=nav}--&gt;
                        &lt;!--{foreach from=$nav key=link_name item=link_url}--&gt;
                        <div class="&lt;!--{cycle values="about,services,contact,services,about"}--&gt;Nav"><a href="&lt;!--{site_url url="home/$link_url"}--&gt;">&lt;!--{$link_name}--&gt;</a></div>
                            &lt;!--{/foreach}--&gt;
                        &lt;!--{/foreach}--&gt;&lt;!--{/if}--&gt;
                        &lt;!--{if empty($navigation)}--&gt;
                            
                        <div class="aboutNav"><a href="&lt;!--{site_url url="home/about"}--&gt;">About</a></div>
    
                        <div class="servicesNav"><a href="&lt;!--{site_url url="home/services"}--&gt;">Services</a></div>  
                        <div class="contactNav"><a href="&lt;!--{site_url url="home/partners"}--&gt;">Partners</a></div>
                        <div class="servicesNav"><a href="&lt;!--{site_url url="home/portfolio"}--&gt;">Portfolio</a></div>
                        <div class="aboutNav"><a href="&lt;!--{site_url url="home/contact"}--&gt;">Contact</a></div>
                            
                        &lt;!--{/if}--&gt;
                        <div class="homeNav"><a href="&lt;!--{site_url url="admin"}--&gt;">Login</a></div>
                        </div>
                    
                    </div>

                
                </div>
Anyway, for some people to look at code makes them think in code. It's good to see different implementations. I hope this helps someone.
#5

[eluser]unsource[/eluser]
You have a syntax-misstake on cms.sql
line 508:
wrong:
Code:
INSERT INTO `pages` VALUES(2, 'About', 10, 'SMARTY HMVC ENVIRON', 'about', 'regular', ', '', 0);

right:
Code:
INSERT INTO `pages` VALUES(2, 'About', 10, 'SMARTY HMVC ENVIRON', 'about', 'regular','', '', 0);

EDIT:
It doesn't work
Ich have al lot of errors:
http://team-rainbow.eu/markus/error.html

I follow the file install
#6

[eluser]Avatar[/eluser]
Thank you for point out the errors. I've been away from the forum for a bit. The errors your getting are becuase your have not defined your directories that it uses inside the templates template_images_dir, template_swf_dir and template_pdf_dir. I will paste my environ.php lib and config for you. (NOTE: I've made it so it takes config vars from file or db) this setup is from db I will also post table structure for you.
app/config/environ.php
Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/*

config_location

The location you want to load your config from

excepts: ,thisfile

*/
//STRING : template files can be located in db or file
//if db is set, then no more variables will be read below it
//when template config is located in db you can map your templates to admin panel for manipulation by user
//file loads $template_name/config/$theme_config_file.EXT for theme config variables
//db bypasses the usage of $template_name/config/$theme_config_file.EXT and uses database for config
$config['template_location']='db';

//templates folder name in filesystem, even if using db templates are stored on the filesystem for smarty to parse
$config['templates_folder']='templates/';

//the settings below this line are only used if template_location is set to file

//current template name
$config['template_name']="EAI";
//template config file name located inside template/config/ directory
$config['theme_config_file']='theme';
app/libraries/environ.php here http://pastebin.com/f1af0bf0f
app/models/template_model.php here http://pastebin.com/f543c595a
db layout structure here http://pastebin.com/f41c12988
default_controller.php here http://pastebin.com/f66433ab4
a module (home module) home.php here http://pastebin.com/f489a58db
app/language/english/site_lang.php here http://pastebin.com/f7c68f282 (autoloaded)

I hope this helps you.
#7

[eluser]unsource[/eluser]
Thans for your Post.

I have find the misstake in config/environ.php
$config['template_location'] = file

Code:
$config['templates_folder']='templates/';

$config['template_name']="EXAMPLE_1";

because $config['template_name'] have the wrong value in the original file.

Edit:
Now I have include the config into the database.

I get this error:
Quote:Fatal error: Call to undefined method Template_Model::get__by_template_id() in C:\xampp\htdocs\CodeIgniter\system\application\libraries\environ.php on line 133

I use your libraries\environ.php .
Line 127 to 187:
http://pastebin.com/m4c26038c

If I user the original libraries\environ.php :
Quote:Fatal error: Call to undefined method Template_Model::get_asset_dirs_by_id() in C:\xampp\htdocs\CodeIgniter\system\application\libraries\environ.php on line 201
#8

[eluser]sikkle[/eluser]
yingyes : man you need to take time to breath in your post, i'm getting old, please help me to be able read your, put space somewhere ! Smile

good contribution.

see ya around.
#9

[eluser]Avatar[/eluser]
sikkle: Thank you, I think you are correct. I will slow down and explain more throughly.

unsource: You can try to insert this data in your site_config table What this function does is first checks where the configuration is coming from. (database or filesystem, if filesystem then this is located inside the template directories config directory). I'm thinking of just using the configuration from the datrabase. Since the templates and pages are located inside the filesystem, just the configuration for all template files and site wide configuration is from database. Here is data for the site_config table. I apologize for taking so long. This is obvious not a fully working example of the environment variables system, but it does work when you have the templates consuming the variables passed into them from ci.

Code:
--
-- Table structure for table `site_config`
--

CREATE TABLE IF NOT EXISTS `site_config` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(128) NOT NULL default '',
  `comment` varchar(255) NOT NULL default '',
  `value` text NOT NULL,
  `category` varchar(32) NOT NULL default '',
  `orderby` int(11) NOT NULL default '0',
  `type` enum('numeric','text','textarea','checkbox','separator','selector','multiselector') NOT NULL default 'text',
  `active` tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;

--
-- Dumping data for table `site_config`
--

INSERT INTO `site_config` (`id`, `name`, `comment`, `value`, `category`, `orderby`, `type`, `active`) VALUES
(1, 'site_name', 'What is your site name?', 'testing', 'site_config', 5, 'text', 1),
(2, 'site_slogan', 'site slogan goes here', 'where stuff actually happens', '', 0, 'text', 0),
(3, 'meta_keywords', 'meta keywords', '', '', 0, 'text', 0),
(4, 'meta_description', 'meta description', '', '', 0, 'text', 0),
(5, 'language', 'site language', 'english', '', 0, 'text', 0),
(6, 'time_offset', '', '', '', 0, 'text', 0),
(7, 'date_format', 'date format', 'd/m/y', '', 0, 'text', 0),
(8, 'template_id', 'current active template name', '10', '', 0, 'text', 0),
(9, 'enable_caching', 'enable caching?', 'TRUE', '', 0, 'text', 0),
(10, 'caching_duration', 'how long to hold cache in minutes', '30', '', 0, 'text', 0),
(11, 'page_size', 'how many results to show on each page', '20', '', 0, 'text', 0),
(12, 'template_name', 'current active template name', 'EXAMPLE_1', '', 0, 'text', 0),
(13, 'templates_folder', 'templates folder name inside app/views/smarty/', 'templates/', '', 0, 'text', 0),
(14, 'load_templates', '', 'layout,\r\npages,\r\nadmin_layout,\r\nadmin_pages', '', 0, 'text', 0);//you need to create tables for these just like layout below

CREATE TABLE IF NOT EXISTS `templates` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL default 'default',
  `preview` varchar(255) NOT NULL default '',
  `description` text NOT NULL,
  `front_page` varchar(255) NOT NULL default '',
  `images_dir` varchar(128) NOT NULL default '',
  `swf_dir` varchar(128) NOT NULL default '',
  `pdf_dir` varchar(128) NOT NULL default '',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `template` (`name`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

--
-- Dumping data for table `templates`
--

INSERT INTO `templates` (`id`, `name`, `preview`, `description`, `front_page`, `images_dir`, `swf_dir`, `pdf_dir`) VALUES
(9, 'EXAMPLE', '', '', '', 'assets/images/', 'assets/swf/', 'assets/pdf/'),
(8, 'default', '', '', '', '', '', ''),
(10, 'EXAMPLE_1', '', '', '', 'assets/images/', 'assets/swf/', 'assets/pdf/');

--
-- Table structure for table `layout`
--

CREATE TABLE IF NOT EXISTS `layout` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(128) NOT NULL default '',
  `template` int(11) NOT NULL default '0',
  `title` varchar(255) NOT NULL default '',
  `url` varchar(255) NOT NULL default '',
  `text` text NOT NULL,
  `order` tinyint(4) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

--
-- Dumping data for table `layout`
--
//you will also need to have admin_layout created and such, as per site_config variable load_templates
//these are actually templates you have to have created on your filesystem otherwise this data would come from config.php inside current template
INSERT INTO `layout` (`id`, `name`, `template`, `title`, `url`, `text`, `order`) VALUES
(1, 'head', 10, 'html header', 'head', '', 0),
(2, 'foot', 10, 'html footer', 'foot', '', 0),
(3, 'banner', 10, 'site banner', 'banner', '', 0),
(4, 'left', 10, 'left column', 'left', '', 0),
(5, 'content', 10, 'page content', 'content', '', 0),
(6, 'right', 10, 'reight column', 'right', '', 0),
(7, 'sections', 10, 'page content sections', 'sections', '', 0);

--
-- Table structure for table `css`
--
//you will also need to create a js table with the same layout as css table as shown below
CREATE TABLE IF NOT EXISTS `css` (
  `id` int(11) NOT NULL auto_increment,
  `template` int(11) NOT NULL default '0',
  `name` varchar(255) NOT NULL default '',
  `url` varchar(255) NOT NULL default '',
  `orderby` int(4) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `css`
--

INSERT INTO `css` (`id`, `template`, `name`, `url`, `orderby`) VALUES
(1, 10, 'css', 'assets/css/css.css', 5),//this dir is relative to the current template dir
(2, 10, 'lightbox.css', 'assets/css/lightbox.css', 10);
#10

[eluser]Avatar[/eluser]
post chars limit exceeded.
I hope this helps you make a working example. I would post more data, but that would include me havng to post my clients whole site. Seeing as the templates use these environment variables to do magical things. Smile Have a great day.




Theme © iAndrew 2016 - Forum software by © MyBB