Part of the EllisLab Network
   
1 of 3
1
Simple view library
Posted: 12 May 2007 09:04 AM   [ Ignore ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

I began to use code igniter a week ago and developing a site became much quicker than before. The only thing i missed was a way to build a page with the least amount of code necessary but flexible enough to use it for every site. It took me a day to come up with a viewfile based library:

- in /system/application/config/config.php i added a section

/*
|--------------------------------------------------------------------------
| Page parts
|--------------------------------------------------------------------------
|
| Configuration for Simple view library
|
| This is the basic configuration :
| $config['page'] = array('master' =>
|                            array('viewfile', array('page','part'));
|
| To add configuration variables for the page files :
| $config['page'] = array('master' =>
|                            array('viewfile', array('page','part')
|                          'parts' =>
|                              array('partview' =>
|                                  array('variablename' => 'variablevalue'))
|                         );
| master as a parts key is the hook for the variables of the master viewfile
| all as a parts key adds the variables to all the viewfiles
|
*/

 

- The library itself

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

class
Sview {

    
function page($parts = '')
    
{
        
// function variables
        
$CI =& get_instance();
        
$config = $CI->config->item('page');
        
$params = array();
        
// add configuration values to master view file
        
if(isset($config['parts']['master']))
        
{
            
foreach($config['parts']['master'] as $k => $v)
            
{
                $params[$k]
= $v;
            
}
        }
        
if(isset($config['parts']['all']))
        
{
            
foreach($config['parts']['all'] as $k => $v)
            
{
                $params[$k]
= $v;
            
}
        }
        
// add dynamic values to master view file
        
foreach($parts as $k => $v)
        
{
            
// values for master view file itself
            
if($k == 'master')
            
{
                
foreach($v as $k2 => $v2)
                
{
                    $params[$k2]
= $v2;
                
}
            }
            
// values for page parts views
            
else
            
{
                 $partarr
= array();
                 foreach(
$config['parts']['all'] as $k3 => $v3)
                 
{
                     $partarr[$k3]
= $v3;
                 
}
                 
if(isset($config['parts'][$k]))
                 
{
                     
if(is_array($v))
                     
{
                         $arr
= array_merge($v,$config['parts'][$k]);
                         foreach(
$arr as $k2 => $v2)
                         
{
                             $partarr[$k2]
= $v2;
                         
}
                     }
                     
else
                     
{
                         
foreach($config['parts'][$k] as $k2 => $v2)
                         
{
                             $partarr[$k2]
= $v2;
                         
}
                     }
                 }
                 
else
                 
{
                     $partarr
= $v;
                 
}
                $params[$k]
= $CI->load->view($k,$partarr,true);
            
}
        }
        
// check if all parts of the page in configuration are present
        
foreach($config['master'][1] as $part)
        
{
            $keys
= array_keys($params);
            if(!
is_numeric(array_search($part,$keys)))
            
{
                $params[$part]
= '';
            
}
        }
        $CI
->load->view($config['master'][0],$params);
    
}

}
?>

I shortened the classname to sview, again to type as little as possible.

In the controller the code can be as simple as :

$this->load->library('sview');
$this->sview->page();

The array to add dynamic values into the has to have the viewfile names as keynames with the extra key master for the master viewfile.

All comments are welcome and i hope someone else can find a use for this library.

Profile
 
 
Posted: 12 May 2007 10:48 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
RankRankRank
Total Posts:  558
Joined  06-17-2006

Thank you.

Can you provide a more complete example of how this will be used with variables and the lot.

I would recommend using its own configuration file rather the the config.php. Is this way you could upgrade CI without losing your changes.

 Signature 

CodeCrafter - Open Source Code Generation for CI

Profile
 
 
Posted: 13 May 2007 03:36 AM   [ Ignore ]   [ # 2 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

I made some changes in the library

- Now it’s possible to have different masterpages.
- The config file is now /application/config/sview.php

I have some basic examples set up.

The library code is now :

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

class
Sview {

    
function page($parts = '',$layout = '')
    
{
        
// function variables
        
$CI =& get_instance();
        include(
APPPATH.'config/sview'.EXT);
        
$keys = array_keys($page);
        if(
$layout == '' || ($layout != '' && !is_numeric(array_search($layout,$keys))))
        
{
            $layout
= 'default';
        
}
        $config
= $page[$layout];
        
$params = array();
        
// add configuration values to master view file
        
if(isset($config['part']['master']))
        
{
            
foreach($config['part']['master'] as $k => $v)
            
{
                $params[$k]
= $v;
            
}
        }
        
if(isset($config['part']['all']))
        
{
            
foreach($config['part']['all'] as $k => $v)
            
{
                $params[$k]
= $v;
            
}
        }
        
// add dynamic values to master view file
        
if(is_array($parts))
        
{
            
foreach($parts as $k => $v)
            
{
                
// values for master view file itself
                
if($k == 'master')
                
{
                    
foreach($v as $k2 => $v2)
                    
{
                        $params[$k2]
= $v2;
                    
}
                }
                
// values for page parts views
                
else
                
{
                     $partarr
= array();
                    if(isset(
$config['part']['all']))
                    
{
                         
foreach($config['part']['all'] as $k3 => $v3)
                         
{
                             $partarr[$k3]
= $v3;
                         
}
                     }
                     
if(isset($config['part'][$k]))
                     
{
                         
if(is_array($v))
                         
{
                             $arr
= array_merge($v,$config['part'][$k]);
                             foreach(
$arr as $k2 => $v2)
                             
{
                                 $partarr[$k2]
= $v2;
                             
}
                         }
                         
else
                         
{
                             
foreach($config['part'][$k] as $k2 => $v2)
                             
{
                                 $partarr[$k2]
= $v2;
                             
}
                         }
                     }
                     
else
                     
{
                         $partarr
= $v;
                     
}
                    $params[$k]
= $CI->load->view($k,$partarr,true);
                
}
            }
        }
        
// check if all parts of the page in configuration are present
        
if(isset($config['part']['view']) && is_array($config['part']['view']))
        
{
            
foreach($config['part']['view'] as $part)
            
{
                $keys
= array_keys($params);
                if(!
is_numeric(array_search($part,$keys)))
                
{
                    $params[$part]
= '';
                
}
            }
        }
        $CI
->load->view($config['master']['view'],$params);
    
}

}
?>

All comments are still welcome.

Profile
 
 
Posted: 14 May 2007 03:32 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  515
Joined  12-05-2006

Great!

why don’t you add this lib in the WIKI?

my suggestion is to put a bit of comments in the class header like:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Library for fast and effective views/layout rendering.
* Set your config values in a config file in /application/config/sview.php
*-----------------------------------------------------------------------------------
* DEMO CONFIG FILE
|--------------------------------------------------------------------------
| Page parts
|--------------------------------------------------------------------------
|
| Configuration for Simple view library
|
| This is the basic configuration :
| $config['page'] = array('master' =>
|                            array('viewfile', array('page','part'));
|
| To add configuration variables for the page files :
| $config['page'] = array('master' =>
|                            array('viewfile', array('page','part')
|                          'parts' =>
|                              array('partview' =>
|                                  array('variablename' => 'variablevalue'))
|                         );
| master as a parts key is the hook for the variables of the master viewfile
| all as a parts key adds the variables to all the viewfiles
*
* "master" is ...
*
* "parts" is ...
*
* "partview" is...
*
*
*-----------------------------------------------------------------------------------
* USAGE EXAMPLE IN A CONTROLLER
*-----------------------------------------------------------------------------------
* $this->load->library('sview');
* $this->sview->page();
*
*-----------------------------------------------------------------------------------
* @package     Sview
* @subpackage  Libraries
* @category    Layout
* @author      Your Name (xwero)
* @copyright   Copyright (c) 2007, your_copyright_stuff
* @license        http://www.gnu.org/licenses/lgpl.html
* @link         http://your_link.com/ or documentation link
* @version     1.0.0
*
*/

// the class here...
 Signature 

FreakAuth_light: pluggable & extendable authentication library that works on CI 1.5.X

CI SWIFT MAILER: 44% less memory than PHPMailer at double speed

Using Zend Framework components in Code Igniter

Profile
 
 
Posted: 14 May 2007 12:54 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
RankRank
Total Posts:  233
Joined  04-03-2007

Thanks for sharing.

Just curious if you considered using YATS?

Profile
 
 
Posted: 14 May 2007 01:48 PM   [ Ignore ]   [ # 5 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

Thanks danfreak for the nice words and i will add comments when i’m satisfied with it. Now it’s still a work in progress. I want to make it work according the CI coding principles. that’s one of the reasons i started this topic

Hey a&w i did take a look at it but it seemed to me like it was too much work to use it. I wanted a simple and quick way to store recurring values based on the already present infrastructure. Maybe if i take a better look at it i can make my library a stepping stone to yats. In my opinion yats is for powerusers wink

Profile
 
 
Posted: 14 May 2007 02:20 PM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  515
Joined  12-05-2006

I give you guys a couple of helpers I’ve written for a project.
I put them in a file called layout_helper.php

These few functions are usefull to load custom js/css needed by specific pages.

Maybe we can build a simple_layout_helper wink
I give you a bit of input…

[EDITED]

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Layout Helper
*
* @package       Simple Layout
* @subpackage  Helpers
* @category       Layout
* @author          Daniel Vecchiato (danfreak)
* @copyright      Copyright (c) 2007, 4webby.com
* @license         http://www.gnu.org/licenses/gpl.html
* @link            http://4webby.com/
* @version         1.0.0-alpha
*/

// --------------------------------------------------------------------
/**
* Outputs the HTML header of a page
* ----------------------------------------------------------------
* USAGE EXAMPLE in CONTROLLER
* ----------------------------------------------------------------
* $this->load->helper(layout);
* $data['page_title']='videos'; //<- STRING
* $this->load->view('your_view_container', $data);
*
* ----------------------------------------------------------------
* USAGE EXAMPLE in HEADER view
* ----------------------------------------------------------------
* <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
* <?=isset($page_title) ? HTMLheader($page_title) : HTMLheader()?>
*
* @param string $page_title
* @return string HTML
*/
function HTMLheader($page_title=null)
{
    $CI
=& get_instance();
    
    
// do we have a page title?!?
    
if (isset($page_title) ? $page_title = ucfirst($page_title).' &raquo; ': '');
    
$header  ='<html xmlns="http://www.w3.org/1999/xhtml" >'."\n";
    
$header .='<head>'."\n";
    
$header .='<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>'."\n";
    
$header .='<title>'.$page_title.$CI->lang->line('EUS_header_title').'</title>'."\n";
    
$header .='<meta name="keywords" content="'.$CI->lang->line('EUS_header_keywords').'"/>'."\n";
    
$header .='<meta name="description" content="'.$CI->lang->line('EUS_header_description').'" />'."\n";
    return
$header;
}

// --------------------------------------------------------------------
/**
* Javascript file loader in header
* You can pass either a string or an array as input.
* The input must contain the names of the js files without dots and js extension.
* The files must be located in your website_root/public/js/ folder
*
* ----------------------------------------------------------------
* USAGE EXAMPLE in CONTROLLER
* ----------------------------------------------------------------
* $this->load->helper(layout);
* $data['js']=load_js(array('home', 'jquery.innerfade')); //<- ARRAY
* $data['js']=load_js('jquery.innerfade'); //<- STRING
* $this->load->view('your_view_container', $data);
* ----------------------------------------------------------------
* USAGE EXAMPLE in HEADER view
* ----------------------------------------------------------------
* <?=isset($js )? $js: ''?>
*
* @param string $js it can also be an array
* @return string html formatted
*/
function load_js($js='')
{    
    $js_header
= '';
    if (
$js!='')
    
{
        
if (is_array($js))
        
{
            $js_header
= '';
            foreach(
$js as $value)
            
{
                
//change the beginning ">" tag to "<": the forum strips js tags
                               
$js_header .= '>script type="text/javascript" src="'.base_url().'public/js/'.$value.'.js"></script>'."\n";
            
}    
        }
        
else
        
{
                        
//change the beginning ">" tag to "<": the forum strips js tags
            
$js_header = '>script type="text/javascript" src="'.base_url().'public/js/'.$js.'.js"></script>'."\n";
        
}
    }
    
return $js_header;
}

continued…

 Signature 

FreakAuth_light: pluggable & extendable authentication library that works on CI 1.5.X

CI SWIFT MAILER: 44% less memory than PHPMailer at double speed

Using Zend Framework components in Code Igniter

Profile
 
 
Posted: 16 May 2007 03:17 AM   [ Ignore ]   [ # 7 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  104
Joined  03-02-2007

The HTML Headers are lacking a DOCTYPE, a critical part (unless you just love quirks mode, but nobody really does).

Also, a specification of how to include CSS files would be nice. There’s your approach, but a lot of people also use the standard <link rel=‘stylesheet’...> method, which raises another question - include as ‘stylesheet’ or ‘alternate stylesheet’ (for style-switching).

Also, something seems to be missing from you js_header - it only includes “</script>\n” and nothing more.

 Signature 

- Dragon Silhouette - Rebecca Kemp
- Conceptivator.com
- Hamachidota.com

Profile
 
 
Posted: 16 May 2007 03:26 AM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  515
Joined  12-05-2006

Hey Fayra, you right, I didn’t post my main header:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?=isset($page_title) ? HTMLheader($page_title) : HTMLheader()?>
<link type="text/css" media="all" rel="stylesheet" href="<?=base_url();?>public/css/eusarf.css"/>
<?=isset($css )? $css: ''?>
>script type="text/javascript" src="<?=base_url();?>public/js/jquery.js"></script>
>script type="text/javascript" src="<?=base_url();?>public/js/flash.js"></script>
<?
=isset($js )? $js: ''?>

The problem with js is that the forum strips out the js tags!
Cahnge the beginning is tag from “>” to “<

I repost the CSS part I previously posted above

<?php
// --------------------------------------------------------------------
/**
* CSS file loader in header
* You can pass either a string or an array as input.
* The input must contain the names of the css files without dots and css extension.
* The files must be located in your website_root/public/css/ folder
*
* ----------------------------------------------------------------
* USAGE EXAMPLE in CONTROLLER
* ----------------------------------------------------------------
* $this->load->helper(layout);
* $data['css']=load_css(array('home', 'forms')); //<- ARRAY
* $data['css']=load_css('home'); //<- STRING
* $this->load->view('your_view_container', $data);
* ----------------------------------------------------------------
* USAGE EXAMPLE in HEADER view
* ----------------------------------------------------------------
* <?=isset($css )? $css: ''?>
*
* @param string $css it can also be an array
* @return string html formatted
*/
function load_css($css='')
{    
    $css_header
= '';
    if (
$css!='')
    
{
        
if (is_array($css))
        
{
            $css_header
= '';
            foreach(
$css as $value)
            
{
                $css_header
.= '<style type="text/css" media="all">@import "'.base_url().'public/css/'.$value.'.css";</style>'."\n";
            
}    
        }
        
else
        
{
            $css_header
.= '<style type="text/css" media="all">@import "'.base_url().'public/css/'.$css.'.css";</style>'."\n";
        
}
    }
    
return $css_header;
}
 Signature 

FreakAuth_light: pluggable & extendable authentication library that works on CI 1.5.X

CI SWIFT MAILER: 44% less memory than PHPMailer at double speed

Using Zend Framework components in Code Igniter

Profile
 
 
Posted: 16 May 2007 03:49 AM   [ Ignore ]   [ # 9 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  515
Joined  12-05-2006
Flayra - 16 May 2007 03:17 AM

Also, a specification of how to include CSS files would be nice.

 

Well I put that in the function comments.
I use that function for CUSTOM CSS needed only by certain pages (i.e. I used them for the jquery lightbox css only in the pages using Lightbox) etc.
CSS always needed by the app should rather be included staticaly-> see header posted above

 Signature 

FreakAuth_light: pluggable & extendable authentication library that works on CI 1.5.X

CI SWIFT MAILER: 44% less memory than PHPMailer at double speed

Using Zend Framework components in Code Igniter

Profile
 
 
Posted: 16 May 2007 08:11 PM   [ Ignore ]   [ # 10 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

I’m not so found on having to rely on php for html output, I like to keep it the view files. But is certainly an addition for rapid development.

I’m currently working on the possibility to allow one configuration for different pageparts.
Possible configuration :

$page['default']['part']['content1 content2']['img'] = 'test.jpg';

I’m also are going to add the configuration options to the loaded configuration array so that it can be used outside the library.
Possible use :

$layout = $this->config->item('sview','default');
$this->loggingmodel->count_file_size($layout['master']['headerimg']);

I hope i can add the changes this weekend, I will also add comments to the code.

Profile
 
 
   
1 of 3
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120614 Total Logged-in Users: 65
Total Topics: 126649 Total Anonymous Users: 2
Total Replies: 665772 Total Guests: 539
Total Posts: 792421    
Members ( View Memberlist )