FUEL CMS User Guide : Version 1.5.2


Opt-in Controller Development

Do you ever feel that its a little silly to create a controller method to render even the simplest page? Opt-in Controller Development speeds up development by allowing you to create your pages without using controllers. Variables can be passed to view files similar to how CI's routes work or can be set in the page itself and will cascade to all subviews of the page. You can even load in a library, model and helper. This method also allows for deeper website structures without the need for special routing to controllers.

How it Works

The CodeIgniter Router is modified to route all requests that don't map to a controller method to a default controller named page_router. This controller will do one of two things, depending on the fuel_mode value set in the FUEL config file:

  1. render pages saved in the fuel database (not what we are doing here)
  2. find a corresponding view file to the uri requested and map variables to the view file found the views/_variables folder

What is a Variables File?

A variables file is an array of variables that get passed to pages. The files are located in the views/_variables folder

There are three levels of variables:

Global Variables

Global variables can be set in the views/_variables/global.php file using the $vars array variable. Often times you will set default page variables in this file. By default, the following global variables are provided:

$vars['layout'] = 'main';
$vars['page_title'] = '';
$vars['meta_keywords'] = '';
$vars['meta_description'] = '';
$vars['js'] = '';
$vars['css'] = '';
$vars['body_class'] = uri_segment(1).' '.uri_segment(2);

Controller Variables

Controller variables are applied to what would normally be the controller level of a website. For example, If you have an about section of your website at http://www.mysite.com/about, you can create a views/_variables/about.php a variables file at views/_variables/about.php can be used. Variables in this file will overwrite any global variable with the same key. The variables will be extended automatically to all sub pages of the about section (e.g. http://www.mysite.com/about/contact) . Variables at the controller level use the same $vars variable as global variables.

$vars['layout'] = 'level2';
$vars['page_title'] = 'About : My Website';
$vars['body_class'] = 'about';

Page Variables

Page variables are applied to a specific URI path. They will overwrite any global or controller variable set with the same key. As an example, you may want to overwrite the page_title of About : My Website to Contact : My Website.. To do this, you can create a $pages array variable in your global or controller variables file (e.g. views/_variables/about.php) with the key being the URI of the page or a regular expression to match pages (just like routing files). The value is an array of variables to pass to the page.

// controller level variables
$vars['layout'] = 'level2';
$vars['page_title'] = 'About : My Website';
$vars['body_class'] = 'about';

// page specific
$pages['about/contact'] = array('page_title' => 'Contact : My Website');
$pages['about/contact$|about/careers$'] = array('layout' => 'special');

Adding Variables from Within the View

An alternative to creating $pages variables is to simply declare the variable in the view file using the fuel helper function fuel_set_var. By setting the variable this way, you will ensure that the variable will also get passed to the layout file containing the view file.

<?php fuel_set_var('page_title', 'About : My Website'); ?>

<h1>About My Website</h1>
<p>A long, long, time ago, in a galaxy far, far away...</p>

Using Variable Files Within a Controller

If perhaps you need to use the variables in a controller (e.g. you have a form located at about/contact and an about controller variables file), you can load it like so:

class About extends CI_Controller {
	
	function __construct()
	{
		parent::__construct();
	}
	
	function contact()
	{
		// set your variables
		$vars = array('page_title' => 'Contact : My Website');

		//... form code goes here
		
		// use Fuel_page to render so it will grab all opt-in variables and do any necessary parsing
		$this->fuel->pages->render('about/contact', $vars);
	}

}

Special Variables

The following are special variable keys that can be used in the variables files: