Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 17 revisions

Category:Libraries Category:Libraries::Template Engines

[size=4]Introduction[/size]

Looking at the other template libraries and the native CI way of displaying pages i didn't find anything that was easy to use and yet powerful enough to handle complex lay-outs.

I didn't want to restrict developers to use a formatted file structure or a database. the only thing the library needs are the two library files and a config file.

[size=4]The files[/size]

[size=3]libraries/Simpleview.php[/size] This is the main library file. The class has two methods:

  • view : which you use in the controllers to display the layout you want.

  • ci_replace : this is a helper function to get data from various CI resources defining them in the config file. For now it supports: -- config->item() use : ConfigItem-xxx| -- argumentless config functions use : ConfigFunct-xxx| -- argumentless uri functions use : UriFunct-xxx| -- session->userdata() use : SessionItem-xxx|

[size=3]libraries/MY_Config[/size] This file contains one method that is used in the main library file:

  • items : it looks for similar named variables in the config array using a regular expression

[size=3]config/simpleview.php[/size] This file holds all the configuration variables.The variable names are divided into sections using an underscore. The variables are divided into three sections : page parts, general variables and partial variables

  • Part parts : there are two prefixes used: -- partials_ : this is a comma delimited string which holds the variable names used in the master pages. The second part has to be the file name of the master view file, for example [code]$config['partials_layout'] = 'header,main,footer';[/code] -- part_ : these variables define the default view files, for example [code]$config['part_main'] = 'main';[/code]

  • general and partial variables : the prefix used for these variables is var_. A general variable has 3 sections and a partial variables 4. the second section is the master view file and the third section is the partial view file. The last section is always the variable name used in the view file(s). general example : [code]$config['var_layout_pagetitle'] = 'Welcome';[/code] partial example : [code]$config['var_layout_main_title'] = 'Hello';[/code]

tip 1 : if you have several variables with the same content make a variable and use that instead of defining each variable seperate. [code]$emptystr = ''; $config['var_layout_main_input1'] = $emptystr; $config['var_layout_main_input2'] = $emptystr;[/code]

tip 2 : if you have view files in a deep filestructure define a variable to shorten the variable name. [code]$deepview = 'folder1/folder2/folder3/view'; $config['var_layout_'.deepview.'input1'] = ''; $config['var_layout'.deepview.'_input2'] = '';[/code]

[size=3]views/layout.php[/size] As this master view file is defined as default in the library. For now the master view file can only be changed in the controllers.

[size=4]Using the library[/size] I explained the use of the config file above so now we are going to explore how to view all that configuring on the website.

In the config/autoload.php file add Simpleview to the libraries array and simpleview to the config array.

To view the page with the default configuration just add [code]$this->simpleview->view();[/code]

The view method has two arguments: $vars and $parts

  • $vars is an array that can be used for general and partial variables. General variables have as the first key an empty string, for the partial variables the first key is the variable name used in the master view file. The second key is the variable name. [code]$var['']['pagetitle'] = 'Welcome 2'; $var['main']['input1'] = 'name';[/code]

  • $parts is an array where the first key is the variable name used in the master view file. A predefined variable master is used to switch the master view file. [code]$parts['main'] = 'main2'; $parts['master'] = 'layout2';[/code]

the $vars array is the first argument an the $parts array the second.

[size=4]Constraints[/size]

  • You can't use underscores in folder- and filenames

[size=4]To do[/size]

  • page caching
  • set default master view file in the config file
  • better handling of files with deep filestructure in the config file
  • add more CI generated data placeholders for the config file
  • Add argument to the view method to automatically change the view files if someone has got a different status.

[size=4]Download[/size] 1.0.1 File:simpleview1.0.1.zip

  • Changed libraries/Simpleview.php to allow the master view file in folders 1.0.0 File:simpleview.zip
Clone this wiki locally