<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bruno Matthys dot com</title>
	<atom:link href="http://www.brunomatthys.com/wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brunomatthys.com/wordpress</link>
	<description>Blogging About Web Development</description>
	<lastBuildDate>Mon, 10 Oct 2011 02:26:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Setting up a caching system with CodeIgniter</title>
		<link>http://www.brunomatthys.com/wordpress/2011/10/10/setting-up-a-caching-system-with-codeigniter/</link>
		<comments>http://www.brunomatthys.com/wordpress/2011/10/10/setting-up-a-caching-system-with-codeigniter/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 02:20:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[cache]]></category>

		<guid isPermaLink="false">http://www.brunomatthys.com/wordpress/?p=248</guid>
		<description><![CDATA[Caching for CodeIgniter is not great, the standard functionality allows you to either remove all cached files, or nothing at all. So once again it is time to look for a better alternative. Luckily we have another great addition from Phil Sturgeon, a custom caching library. Make sure your application/cache folder is writable &#8211; for [...]]]></description>
			<content:encoded><![CDATA[<h3>Caching for CodeIgniter is not great, the standard functionality allows you to either remove all cached files, or nothing at all. So once again it is time to look for a better alternative.</h3>
<p>Luckily we have another great addition from Phil Sturgeon, a <a href="http://philsturgeon.co.uk/code/codeigniter-cache">custom caching library</a>.</p>
<p>Make sure your <code>application/cache</code> folder is writable &#8211; for me 755 works but this might not be the case for you. I&#8217;ve also added an .htaccess file with <code>deny from all</code> inside it.</p>
<p>Let&#8217;s build on a previous example I posted some time ago: <a href="http://www.brunomatthys.com/wordpress/2011/08/30/making-global-config-data-available-from-database-in-codeigniter/">the settings model</a>, where site settings are loaded from the database each time a page loads. This might cause performance issues when the project gets bigger and I&#8217;m definitely not a huge fan of squandering resources, being the perfectionist that I am.</p>
<p>Here is our reworked Settings_model:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Settings_model extends CI_Model {

    public static $db_config = array();

    public function __construct() {
        parent::__construct();

        $this-&gt;_load_settings();
    }

    /**
     *
     * _load_settings: load the settings from database
     *
     *
     */

    protected function _load_settings() {
        $this-&gt;load-&gt;library('cache');
        $data = $this-&gt;cache-&gt;get('settings');
        if (empty($data)) {
            $this-&gt;db-&gt;select('login_enabled, register_enabled, install_enabled, members_per_page, admin_email, home_page, default_theme,
            login_attempts, recaptcha_theme, email_protocol, sendmail_path, smtp_host, smtp_port, smtp_user, smtp_pass, site_title, cookie_expires,
            password_link_expires, activation_link_expires, disable_all');
            $this-&gt;db-&gt;from('settings');
            $this-&gt;db-&gt;limit(1);
            $query = $this-&gt;db-&gt;get();

            if($query-&gt;num_rows() == 1) {
                $row = $query-&gt;row();
                self::$db_config['login_enabled'] = $row-&gt;login_enabled;
                self::$db_config['registration_enabled'] = $row-&gt;register_enabled;
                self::$db_config['install_enabled'] = $row-&gt;install_enabled;
                self::$db_config['members_per_page'] = $row-&gt;members_per_page;
                self::$db_config['admin_email_address'] = $row-&gt;admin_email;
                self::$db_config['home_page'] = $row-&gt;home_page;
                self::$db_config['default_theme'] = $row-&gt;default_theme;
                self::$db_config['login_attempts'] = $row-&gt;login_attempts;
                self::$db_config['email_protocol'] = $row-&gt;email_protocol;
                self::$db_config['sendmail_path'] = $row-&gt;sendmail_path;
                self::$db_config['smtp_host'] = $row-&gt;smtp_host;
                self::$db_config['smtp_port'] = $row-&gt;smtp_port;
                self::$db_config['smtp_user'] = $row-&gt;smtp_user;
                self::$db_config['smtp_pass'] = $row-&gt;smtp_pass;
                self::$db_config['site_title'] = $row-&gt;site_title;
                self::$db_config['recaptcha_theme'] = $row-&gt;recaptcha_theme;
                self::$db_config['cookie_expires'] = $row-&gt;cookie_expires;
                self::$db_config['password_link_expires'] = $row-&gt;password_link_expires;
                self::$db_config['activation_link_expires'] = $row-&gt;activation_link_expires;
                self::$db_config['disable_all'] = $row-&gt;disable_all;

                $this-&gt;cache-&gt;write(self::$db_config, 'settings');
            }
        }else{
            self::$db_config['login_enabled'] = $data['login_enabled'];
            self::$db_config['registration_enabled'] = $data['registration_enabled'];
            self::$db_config['install_enabled'] = $data['install_enabled'];
            self::$db_config['members_per_page'] = $data['members_per_page'];
            self::$db_config['admin_email_address'] = $data['admin_email_address'];
            self::$db_config['home_page'] = $data['home_page'];
            self::$db_config['default_theme'] = $data['default_theme'];
            self::$db_config['login_attempts'] = $data['login_attempts'];
            self::$db_config['email_protocol'] = $data['email_protocol'];
            self::$db_config['sendmail_path'] = $data['sendmail_path'];
            self::$db_config['smtp_host'] = $data['smtp_host'];
            self::$db_config['smtp_port'] = $data['smtp_port'];
            self::$db_config['smtp_user'] = $data['smtp_user'];
            self::$db_config['smtp_pass'] = $data['smtp_pass'];
            self::$db_config['site_title'] = $data['site_title'];
            self::$db_config['recaptcha_theme'] = $data['recaptcha_theme'];
            self::$db_config['cookie_expires'] = $data['cookie_expires'];
            self::$db_config['password_link_expires'] = $data['password_link_expires'];
            self::$db_config['activation_link_expires'] = $data['activation_link_expires'];
            self::$db_config['disable_all'] = $data['disable_all'];
        }
    }

}

/* End of file settings_model.php */
/* Location: ./application/models/system/settings_model.php */
</pre>
<p>First of all, follow the installation instructions that come with Phil&#8217;s custom library. Basically you need to copy 2 files into your project.</p>
<p>We start off by loading the cache library and get the current data from our settings cache:</p>
<pre class="brush: php; title: ; notranslate">
$this-&gt;load-&gt;library('cache');
$data = $this-&gt;cache-&gt;get('settings');
</pre>
<p>If there is data (string) then we won&#8217;t load the usual database query and load the cache variables into the static array. If there is no data then we just run the query as usual and we load the result into a cache file:</p>
<pre class="brush: php; title: ; notranslate">
$this-&gt;cache-&gt;write(self::$db_config, 'settings');
</pre>
<p>That&#8217;s all. I remove the cache right before saving the settings in another controller class called <code>Site_settings</code>. I use that class to &#8211; believe it or not &#8211; show and edit the site settings. Deleting the cache:</p>
<pre class="brush: php; title: ; notranslate">
$this-&gt;cache-&gt;delete('settings');
</pre>
<p>Very easy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brunomatthys.com/wordpress/2011/10/10/setting-up-a-caching-system-with-codeigniter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending the CodeIgniter form validation library</title>
		<link>http://www.brunomatthys.com/wordpress/2011/09/13/extending-the-codeigniter-form-validation-library/</link>
		<comments>http://www.brunomatthys.com/wordpress/2011/09/13/extending-the-codeigniter-form-validation-library/#comments</comments>
		<pubDate>Tue, 13 Sep 2011 03:07:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[extending]]></category>

		<guid isPermaLink="false">http://www.brunomatthys.com/wordpress/?p=230</guid>
		<description><![CDATA[Form validation has many faces and the default CodeIgniter set of validation rules just doesn&#8217;t cut it for me, or anyone. Luckily it&#8217;s very easy to extend the standard library, making it possible to add any kind of validation you can imagine. If you go to system/libraries/Form_validation.php you can have a look at all the [...]]]></description>
			<content:encoded><![CDATA[<h3>Form validation has many faces and the default CodeIgniter set of validation rules just doesn&#8217;t cut it for me, or anyone. Luckily it&#8217;s very easy to extend the standard library, making it possible to add any kind of validation you can imagine.</h3>
<p>
If you go to <code>system/libraries/Form_validation.php</code> you can have a look at all the available methods that make up of the default validation. These are off course also <a href="http://codeigniter.com/user_guide/libraries/form_validation.html" target="_blank">described here in the manual</a>, but since we&#8217;re extending this library it could be nice to know what exactly we are extending.
</p>
<p>
When you extend a library, your new class should go in <code>application/libraries</code>. The file name is very important! It needs to be called exactly the same as the class name. Furthermore, you need to add <code>MY_</code> before it.<br />
My file is called <code>MY_Form_validation.php</code> and so is my class. Not complying with these rules will give you headackes because no error will tell you that there is an issue with your naming.</code>
</p>
<p>Code example:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {

    public function __construct()
    {
        parent::__construct();
    }

    /**
     *
     * is_valid_username: verify validity of username against regular expression: a-z, A-Z, 0-9, _, - are allowed
     *
     * @param string $username the username to be validated
     * @return boolean
     *
     */

    public function is_valid_username($username) {
        if (preg_match(&quot;/^[a-zA-Z0-9_-]+$/&quot;, $username)) {
            return true;
        }
        return false;
    }

     /**
     *
     * is_existing_unique_field: check for the existence of a unique field within a database table column
     *
     * @param string $field_value value to be examined
     * @param string $table database table to examine
     * @param string $column_name table column name
     * @return boolean
     *
     */

    public function is_existing_unique_field($value, $info) {

        list($table, $column) = explode('.', $info, 2);

        $this-&gt;CI-&gt;db-&gt;select($column);
        $this-&gt;CI-&gt;db-&gt;from($table);
        $this-&gt;CI-&gt;db-&gt;where($column, strtolower($value));
        $this-&gt;CI-&gt;db-&gt;limit(1);

        $query = $this-&gt;CI-&gt;db-&gt;get();

        if($query-&gt;num_rows() == 0) {
            return true;
        }
        return false;
    }

}
</pre>
<p>This example has a method that will check whether the username fits our naming convention and a method that will check whether the username is taken or not.<br />
<br />
Then in our controller we simply add it in our form validation with set_rules():
</p>
<pre class="brush: php; title: ; notranslate">
$this-&gt;form_validation-&gt;set_rules('username', 'username',
'trim|required|max_length[16]|min_length[6]|is_valid_username|is_existing_unique_field[user.username]');
</pre>
<p>The first function, <code>is_valid_username</code>, is very simple. The second one, <code>is_existing_unique_field</code> uses a technique I found on some website, I don't know where I found it so sadly I can't show you the original post. Since we can send only one parameter through to the method the trick is to split a string up into usable chunks (explode) and then use these to query the database.</p>
<p>All in all very easy, in case it's not clear just leave a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brunomatthys.com/wordpress/2011/09/13/extending-the-codeigniter-form-validation-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CodeIgniter Membership script now online</title>
		<link>http://www.brunomatthys.com/wordpress/2011/09/12/codeigniter-membership-script-now-online/</link>
		<comments>http://www.brunomatthys.com/wordpress/2011/09/12/codeigniter-membership-script-now-online/#comments</comments>
		<pubDate>Mon, 12 Sep 2011 02:58:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[login script]]></category>

		<guid isPermaLink="false">http://www.brunomatthys.com/wordpress/?p=216</guid>
		<description><![CDATA[My latest CodeIgniter project is now online. I&#8217;ve been working on this for the past couple of weeks. CI_Membership is my first product for CodeCanyon, a popular network for developers to sell their work on. I&#8217;m curious to see how this will evolve. In the meantime I&#8217;m continuing work on version 1.1 to further improve [...]]]></description>
			<content:encoded><![CDATA[<h3>My latest CodeIgniter project is now online. I&#8217;ve been working on this for the past couple of weeks. CI_Membership is my first product for <a href="http://codecanyon.net" target="_blank">CodeCanyon</a>, a popular network for developers to sell their work on. I&#8217;m curious to see how this will evolve. In the meantime I&#8217;m continuing work on version 1.1 to further improve the package.</h3>
<p><a href="http://codecanyon.net/item/codeigniter-membership-script/526888" target="_blank"><img src="http://www.brunomatthys.com/wordpress/wp-content/uploads/2011/09/CI_Membership.jpg" alt="" title="CI_Membership" width="590" height="300" class="aligncenter size-full wp-image-218" /></a></p>
<p><a href="http://codecanyon.net/item/codeigniter-membership-script/526888" target="_blank">CI_Membership</a> is a registration script made in CodeIgniter that has a few nice features:</p>
<ol style="padding-left: 30px">
<li>highly secure password algorithm with salt and key in a 128-long encrypted variable</li>
<li>separate password and username retrieval</li>
<li>resend activation link</li>
<li>jQuery as well as PHP field validation</li>
<li>3 ways to configure mail setup: PHP mail(), sendmail and SMTP</li>
<li>database sessions and site settings</li>
<li>members can edit their profile</li>
<li>simple roles system</li>
<li>theming</li>
<li>focus is on easy to understand, quick and clean coding</li>
</ol>
<p>I&#8217;ve worked long and hard on this which is why I&#8217;ve decided to place it on a reseller website. I&#8217;m going to maintain and expand this product from now on and buyers will get all updates for free. Price might go up as features are added.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brunomatthys.com/wordpress/2011/09/12/codeigniter-membership-script-now-online/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making global config data available from database in Codeigniter</title>
		<link>http://www.brunomatthys.com/wordpress/2011/08/30/making-global-config-data-available-from-database-in-codeigniter/</link>
		<comments>http://www.brunomatthys.com/wordpress/2011/08/30/making-global-config-data-available-from-database-in-codeigniter/#comments</comments>
		<pubDate>Tue, 30 Aug 2011 23:48:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.brunomatthys.com/wordpress/?p=200</guid>
		<description><![CDATA[It&#8217;s been a while since I wrote my last article, mostly because I&#8217;m working hard on this private project and have to spend all my time on it. This article discusses a way of setting global configuration options from database, available through the whole website. I&#8217;d still like to share with you this interesting bit [...]]]></description>
			<content:encoded><![CDATA[<h3>It&#8217;s been a while since I wrote my last article, mostly because I&#8217;m working hard on this private project and have to spend all my time on it. This article discusses a way of setting global configuration options from database, available through the whole website.</h3>
<p>I&#8217;d still like to share with you this interesting bit of coding. I&#8217;m using an <strong>auto-loaded model to set some static variables which have been pulled from the database</strong>. This database has a <strong>table with one row of data which holds all config data</strong>.</p>
<p>Have a look at the <strong>Settings_model</strong>:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Settings_model extends CI_Model {

    public static $db_config;

    public function __construct() {
        parent::__construct();
        $this-&gt;_load_settings();
    }

    /**
     *
     * _load_settings: load the settings from database
     *
     *
     */

    protected function _load_settings() {
        $this-&gt;db-&gt;select('login_enabled, register_enabled, members_per_page, admin_email, home_page, default_theme,
        login_attempts, email_protocol, sendmail_path, smtp_host, smtp_port, smtp_user, smtp_pass, site_title');
        $this-&gt;db-&gt;from('settings');
        $this-&gt;db-&gt;limit(1);
        $query = $this-&gt;db-&gt;get();

        if($query-&gt;num_rows() == 1) {
            $row = $query-&gt;row();
            self::$db_config['login_enabled'] = $row-&gt;login_enabled;
            self::$db_config['registration_enabled'] = $row-&gt;register_enabled;
            self::$db_config['members_per_page'] = $row-&gt;members_per_page;
            self::$db_config['admin_email_address'] = $row-&gt;admin_email;
            self::$db_config['home_page'] = $row-&gt;home_page;
            self::$db_config['default_theme'] = $row-&gt;default_theme;
            self::$db_config['login_attempts'] = $row-&gt;login_attempts;
            self::$db_config['email_protocol'] = $row-&gt;email_protocol;
            self::$db_config['sendmail_path'] = $row-&gt;sendmail_path;
            self::$db_config['smtp_host'] = $row-&gt;smtp_host;
            self::$db_config['smtp_port'] = $row-&gt;smtp_port;
            self::$db_config['smtp_user'] = $row-&gt;smtp_user;
            self::$db_config['smtp_pass'] = $row-&gt;smtp_pass;
            self::$db_config['site_title'] = $row-&gt;site_title;
        }
    }

}

/* End of file settings_model.php */
/* Location: ./application/models/settings_model.php */
</pre>
<p>As you can see I&#8217;m using a static array called <code>$db_config</code> and to make sure it is available I&#8217;m also autoloading it in <code>config/autoload.php</code>. </p>
<pre class="brush: php; title: ; notranslate">
$autoload['model'] = array('system/settings_model');
</pre>
<p>Now if you need to access a variable all you need to do is:</p>
<pre class="brush: php; title: ; notranslate">
print &quot;&lt;div class=\&quot;input_div\&quot;&gt;Site title &amp;raquo; &quot;.
form_input(array('name' =&gt; 'site_title', 'id' =&gt; 'site_title', 'class' =&gt; 'input_text', 'value' =&gt; Settings_model::$db_config['site_title'])) .&quot;&lt;/div&gt;\r\n&quot;;
</pre>
<p>This should be straight-forward enough but do comment in case of any questions or remarks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brunomatthys.com/wordpress/2011/08/30/making-global-config-data-available-from-database-in-codeigniter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery.getJSON() and sending special characters to PHP through a URL</title>
		<link>http://www.brunomatthys.com/wordpress/2011/07/01/jquery-getjson-and-sending-special-characters-to-php-through-a-url/</link>
		<comments>http://www.brunomatthys.com/wordpress/2011/07/01/jquery-getjson-and-sending-special-characters-to-php-through-a-url/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 21:11:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[decode]]></category>
		<category><![CDATA[encode]]></category>
		<category><![CDATA[getJSON]]></category>

		<guid isPermaLink="false">http://www.brunomatthys.com/wordpress/?p=177</guid>
		<description><![CDATA[A quick word on sending JSON requests that might possibly contain special characters. We&#8217;re going to send this data through a URL to our PHP script so we need to have some kind of encoding/decoding structure in place for it to work. First off, the encodeURIComponent() JavaScript function takes care of converting special characters to [...]]]></description>
			<content:encoded><![CDATA[<h3>A quick word on sending JSON requests that might possibly contain special characters. We&#8217;re going to send this data through a URL to our PHP script so we need to have some kind of encoding/decoding structure in place for it to work.</h3>
<p>First off, the <a href="http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp" target="_blank">encodeURIComponent()</a> JavaScript function takes care of converting special characters to their %-based equivalent. This means that we can safely use them inside our getJSON function like this:</p>
<pre class="brush: jscript; title: ; notranslate">
$.getJSON('register/is_valid_email/' + encodeURIComponent($(&quot;#email&quot;).val()), function(data) {
    // do stuff
});
</pre>
<p>I&#8217;m calling a PHP function here (CodeIgniter) where the first and only parameter is a string containing an e-mail address.<br />
This function might look like this:</p>
<pre class="brush: php; title: ; notranslate">
public function is_valid_email($email) {
    $email = rawurldecode($email);
    //validate email address any way you prefer
}
</pre>
<p>The <a href="http://www.php.net/manual/en/function.rawurldecode.php" target="_blank">rawurldecode</a> is being used to safely convert the encoded e-mail address back to normal. Conclusion: <strong>encodeURIComponent is the JavaScript counterpart of the PHP rawurldecode function</strong>?</p>
<p>Well, not quite. As Javier states in <a href="http://www.php.net/manual/en/function.rawurldecode.php#80209" target="_blank">his 2008 post on php.net</a>, some characters are not decoded on the PHP side and you should use a custom function to deal with those. As far as I know you can&#8217;t use the characters he mentions in an e-mail address (yet) but <a href="http://www.regular-expressions.info/email.html" target="_blank">validating e-mail addresses is one of the most complicated tasks</a> and the RFCs change from time to time making it wise to keep track of this evolution. It proves again that there&#8217;s always something around the corner and programming is not always an exact science.<br />
Also, check <a href="http://stackoverflow.com/questions/1734250/what-is-the-equivalent-of-javascripts-encodeuricomponent-in-php" target="_blank">this post</a> where it states: &#8220;<strong>encodeURIComponent escapes all characters except the following: alphabetic, decimal digits, &#8211; _ . ! ~ * &#8216; ( )</strong>&#8220;.<br />
After some careful testing I came up with the characters that are not processed by both encodeURIComponent () and PHP&#8217;s rawurldecode(): <strong>! * ( ) % /</strong>, or at least these are the ones where the PHP function did not return an answer, indicating something went wrong. I hope my conclusion is complete but will edit this post should I find holes in my research.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brunomatthys.com/wordpress/2011/07/01/jquery-getjson-and-sending-special-characters-to-php-through-a-url/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Simple Templating System For CodeIgniter</title>
		<link>http://www.brunomatthys.com/wordpress/2011/06/15/a-simple-templating-system-for-codeigniter/</link>
		<comments>http://www.brunomatthys.com/wordpress/2011/06/15/a-simple-templating-system-for-codeigniter/#comments</comments>
		<pubDate>Wed, 15 Jun 2011 23:29:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[login script]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.brunomatthys.com/wordpress/?p=115</guid>
		<description><![CDATA[CodeIgniter is the easiest PHP framework in existence today. It takes 2 seconds to set up and has an extensive documentation library available plus the learning curve is not as steep as for example with Zend Framework. CodeIgniter is lightweight which means you&#8217;ll have to depend on third-party plugins to do some of your work; [...]]]></description>
			<content:encoded><![CDATA[<h3 style="padding-bottom: 25px;"><em>CodeIgniter is the easiest PHP framework in existence today. It takes 2 seconds to set up and has an extensive documentation library available plus the learning curve is not as steep as for example with Zend Framework. CodeIgniter is lightweight which means you&#8217;ll have to depend on third-party plugins to do some of your work; this is also the case for templates and layouts. You could use an engine like <a title="Dwoo" href="http://dwoo.org/" target="_blank">Dwoo</a> or <a title="Twig" href="http://www.twig-project.org/" target="_blank">Twig</a> to give you more power for creating templates but you&#8217;ll have to learn those on top of CodeIgniter and sometimes when writing a software package you&#8217;ll want the end-users to have your code up and running as fast as possible without having to dig into unknown territory. Keeping this into mind we&#8217;re going to set up a very simple and easy to understand layout system that offers the flexibility we need without overwhelming our target audience.</em></h3>
<h3>1. Controller setup</h3>
<p>Let&#8217;s have a look at our <strong>controller </strong>first. I set my default controller to home in <code>config/routes.php</code>. We will define the layout options here which can be used inside our layout files.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {

    function index() {
        // pre-load helpers
        $this-&gt;load-&gt;helper('url');

        // set layout head data
        $layout_data['pageTitle'] = &quot;Home&quot;;
        $layout_data['meta_description'] = &quot;Flexible Registration Script&quot;;
        $layout_data['meta_keywords'] = &quot;add keywords here&quot;;

        // set content data
        $content_data['param1'] = &quot;parameter 1&quot;;
        $content_data['param2'] = &quot;parameter 2&quot;;
        $content_data['param3'] = &quot;parameter 3&quot;;

        // set layout content sections
        $layout_data['header'] = $this-&gt;load-&gt;view('layouts/default/header', '', true);
        $layout_data['content_body'] = $this-&gt;load-&gt;view('pages/homepage', $content_data, true);
        $layout_data['footer'] = $this-&gt;load-&gt;view('layouts/default/footer', '', true);

        // load a layout to the browser
        $this-&gt;load-&gt;view('layouts/default/main', $layout_data);
    }
}

/* End of file home.php */
/* Location: ./application/controllers/home.php */
</pre>
<p>A. We will first load the <strong>url helper</strong> because we require the <code>base_url()</code> function to work in our templates. This is one of those common helpers so you should be auto-loading it anyway.<br />
B. Set some basic data to fill up the <strong>head and meta tags</strong> defined in the layout template discussed later.<br />
C. <strong>Content data</strong> is normally pulled from a <strong>data source</strong> but for the sake of the article we&#8217;re using a pre-defined array.<br />
D. <strong>Defining the content sections</strong> is where it all happens. The following line of code adds a new element in our <code>$layout_data</code> array by using the optional third parameter <code>true</code>. We do this for the content and footer section, too.</p>
<pre class="brush: php; title: ; notranslate">
$layout_data['header'] = $this-&gt;load-&gt;view('layouts/default/header', '', true);
</pre>
<p>E. <strong>Load the view to the browser</strong> and this to our default layout.</p>
<h3>2. Layout setup</h3>
<p>The folder structure used in the views folder:<br />
<img src="http://www.brunomatthys.com/wordpress/wp-content/uploads/2011/06/views_folder.jpg" alt="" title="views_folder" width="182" height="325" class="size-full wp-image-145" /><br />
I&#8217;ve set up a default folder for the main layout. The <code>main.php</code> file contains the html frame where we&#8217;ll be loading in the header and footer sections. The <code>header.php</code> and <code>footer.php</code> file both contain their part of the html file.</p>
<p>An example for <code>main.php</code>:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;title&gt;My website: &lt;?php print $pageTitle ?&gt;&lt;/title&gt;
    &lt;meta name=&quot;description&quot; content=&quot;&lt;?php print $meta_description ?&gt;&quot;/&gt;
    &lt;meta name=&quot;keywords&quot; content=&quot;&lt;?php print $meta_keywords ?&gt;&quot;/&gt;
    &lt;meta name=&quot;robots&quot; content=&quot;index,follow&quot;/&gt;

    &lt;link rel=&quot;stylesheet&quot; href=&quot;&lt;?php print base_url() ?&gt;css/reset.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; /&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;&lt;?php print base_url() ?&gt;css/style.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; /&gt;
 &lt;/head&gt;

&lt;body id=&quot;main&quot;&gt;
&lt;header&gt;
&lt;?php print $header . &quot;\r\n&quot; ?&gt;
&lt;/header&gt;
&lt;div id=&quot;content_column&quot;&gt;

&lt;?php print $content_body ?&gt;

&lt;/div&gt;
&lt;footer&gt;
&lt;?php print $footer . &quot;\r\n&quot;; ?&gt;
&lt;/footer&gt;
&lt;/body&gt;

&lt;/html&gt;
</pre>
<p>As you can see we can <strong>call the array variables directly by name</strong>, after all we did load them in our controller with the line of code below. We&#8217;re loading the header and footer views like this, too because we put them in an array element. This means that your template defines what data the controller should pass through.</p>
<pre class="brush: php; title: ; notranslate">
$this-&gt;load-&gt;view('layouts/default/main', $layout_data);
</pre>
<p>You can expand this flexibility, for example you could also predefine the robots in your controller by adding an extra array element to <code>$layout_data</code> or you could add a sidebar section in your template and define the view to be used in the controller. It all depends on your needs. As you can see, there is a disadvantage to this approach: if you change one thing on your layout, you might have to change all your controllers. This is why we could create a CI library to make this even more flexible. This is outside the scope of this article, though.</p>
<p>An example of a header and footer:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;h1&gt;Default Template Header&lt;/h1&gt;
&lt;nav&gt;
&lt;ul title=&quot;navigation&quot;&gt;
    &lt;li&gt;&lt;a href=&quot;&lt;?php print base_url(); ?&gt;&quot; title=&quot;Home Page&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;&lt;?php print base_url(); ?&gt;about&quot; title=&quot;About Page&quot;&gt;Link A&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;&lt;?php print base_url(); ?&gt;add&quot; title=&quot;Add Lyrics&quot;&gt;Link B&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;&lt;?php print base_url(); ?&gt;login&quot; title=&quot;Home Page&quot;&gt;Login&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;
</pre>
<pre class="brush: xml; title: ; notranslate">
&lt;h1&gt;Default Template Footer&lt;/h1&gt;
&lt;p&gt;This is the footer section&lt;/p&gt;
</pre>
<p>An example of the homepage that we are loading into our <code>content_body</code> variable:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div&gt;
&lt;? $this-&gt;load-&gt;view('generic/welcome_message'); ?&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;p&gt;Display params:&lt;/p&gt;
&lt;p&gt;&lt;?php print $param2; ?&gt;&lt;/p&gt;
&lt;p&gt;&lt;?php print $param3; ?&gt;&lt;/p&gt;
&lt;/div&gt;
</pre>
<p>Loading views with the <code>load->view</code> option is also possible, off course. I&#8217;m loading some generic data that is not part of any specific layout or page. The <code>homepage.php</code> file is sitting in the pages directory because it typically holds only content for that page. But it can be loaded anywhere, really.</p>
<h3>3. Setting up a different view</h3>
<p>The <strong>login controller</strong> has a different header because we don&#8217;t want to show the navigation on this page.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

    function index() {

        // pre-load
        $this-&gt;load-&gt;helper('url');

        // set layout head data
        $layout_data['pageTitle'] = &quot;Login&quot;;
        $layout_data['meta_description'] = &quot;login page&quot;;
        $layout_data['meta_keywords'] = &quot;add keywords here&quot;;

        $body_data['param1'] = &quot;parameter 1&quot;;
        $body_data['param2'] = &quot;parameter 2&quot;;
        $body_data['param3'] = &quot;parameter 3&quot;;

        // set layout sections
        $layout_data['header'] = $this-&gt;load-&gt;view('layouts/login/header', '', true);
        $layout_data['content_body'] = $this-&gt;load-&gt;view('pages/login', $body_data, true);
        $layout_data['footer'] = $this-&gt;load-&gt;view('layouts/default/footer', '', true);

        // load a layout to the browser
        $this-&gt;load-&gt;view('layouts/default/main', $layout_data);
    }

}

/* End of file home.php */
/* Location: ./application/controllers/login.php */
</pre>
<p>The only things that change (in the set layout sections) are: the <strong>header</strong> where we are loading the file from <code>layouts/login/header</code> in stead of <code>layouts/default/header</code> and the <strong>content_body</strong> where we are loading from <code>pages/login</code>. Then, have a look at the login layout view <code>header.php</code> below:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;h1&gt;Login Template Header&lt;/h1&gt;
</pre>
<p>And we also need to create a very simple <code>pages/login.php</code> file:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;p&gt;login page&lt;/p&gt;
</pre>
<p>That&#8217;s all that has to be done: set up a different array in the controller and make sure the files you are loading exist in a well-structured layout folder.</p>
<h3>Conclusion</h3>
<p>This simple example serves to offer you an easy way to have some flexibility. When users download your code, all they&#8217;ll have to do is add new layout files and use an existing controller as a base for new controllers of the same nature. I strongly suggest you to use a template engine when you need better functionality but as stated above, that will increase the learning curve and small projects don&#8217;t always need those.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brunomatthys.com/wordpress/2011/06/15/a-simple-templating-system-for-codeigniter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blog now online</title>
		<link>http://www.brunomatthys.com/wordpress/2011/05/31/blog-now-online/</link>
		<comments>http://www.brunomatthys.com/wordpress/2011/05/31/blog-now-online/#comments</comments>
		<pubDate>Tue, 31 May 2011 23:25:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://www.brunomatthys.com/wordpress/?p=101</guid>
		<description><![CDATA[As you can see I&#8217;ve finished the template for my website, based on the Thematic framework. I&#8217;ll be adding posts here soon.]]></description>
			<content:encoded><![CDATA[<p>As you can see I&#8217;ve finished the template for my website, based on the Thematic framework. I&#8217;ll be adding posts here soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brunomatthys.com/wordpress/2011/05/31/blog-now-online/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

