Libraries: Master
In contrast to the basic CodeIgniter framework, every output of a controller will be surrounded by a master-template. All the handling will be driven by the library "Master" (accesible via $this->master). This library will be autoloaded.
The basic layout of the master template looks roughly like this:
<!DOCTYPE [CONFIGURED DOCTYPE] />
<html lang="[CONFIGURED LANGUAGE]" >
<head>
<title>[CONFIGURED TITLE]</title>
[PREDEFINED AND CONFIGURABLE META INFORMATION]
[STATIC, DYNAMIC AND COMPONENTS CONTENT (head_init)]
[CUSTOM AND AUTOMATICALLY LOADED STYLES AND SCRIPTS (head_main)]
[STATIC, DYNAMIC AND COMPONENTS CONTENT (head_finish)]
</head>
<body>
[STATIC, DYNAMIC AND COMPONENTS CONTENT (body_init)]
[YOUR CONTROLLER OUTPUT (body_main)]
[STATIC, DYNAMIC AND COMPONENTS CONTENT (body_finish)]
</body>
</html>
Default Structure
Adapted from HTML-Framework "HTML5 Boilerplate": The class of the <html> tag contains lt-ie9, lt-ie8 or lt-ie7 depending on the browser used. Here is how the <html> tag looks in detail:
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="<?= $master_lang ?>"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="<?= $master_lang ?>"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9" lang="<?= $master_lang ?>"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="<?= $master_lang ?>"> <!--<![endif]-->
Adapted from HTML-Framework "HTML5 Boilerplate" and recommendations of the CSS-Frameworks "YAML" and "Bootstrap": Meta tags for "X-UA-Compatible" and "viewport" used, for cross-browser compatibility, normalizing, building responsive designs and using chromeframe if possible:
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Basic meta information can be configured and will always be included:
<meta charset="[CONFIGURED CHARSET]" />
<meta name="description" content="[CONFIGURED DESCRIPTION]">
<meta name="author" content="[CONFIGURED AUTHOR]">
Setting <base> for portable linking ressources. You do not have to use <?=base_url()?> anymore:
<base href="<?= base_url() ?>" />
Adapted from HTML-Framework "HTML5 Boilerplate": Show legacy info if IE7 is used. This is included in the beginning of <body>:
<!--[if lt IE 7]>
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
<![endif]-->
Components Concept
Components are sets of content for the <head> and/or <body> section, that can be enabled or disabled by config or by code. Components can depend on each other. Enabling one component will enable all depending components, too. Disabling a component, will disable all other depending components, if not used otherwise. Components will always be included before other components depends on it. Supplied components by codecoupler are for example "jQuery", "jQuery-UI", "yaml", "normalize.css" and many others.
You can find more details to this topic, an overview of all components, their keys and how to add you own components here: Libraries: Components
Enable/Disable by Configuration
Components can be enabled in application/config/master.php. Every component have it's own variable like this:
$config['master_components']['NAME_OF_COMPONENT']['enabled']Setting this variable to TRUE, will enable the component.
Enable/Disable Dynamicaly
Everywhere in your code you can enable or disable components. Use the following functions to handle components:
//Enable component. Use the name of the component as $key:
$this->master->enable_component($key);
//Disable component. Use the name of the component as $key.
$this->master->disable_component($key);
//Disable all components:
$this->master->disable_all_components();
//Returns TRUE if the component is enabled, otherwise FALSE. Use the name of the component as $key:
$this->master->is_component_enabled($key);Advanced Configuration
Components can be configured in application/config/components.php. Every component will be described in one array with three keys:
- config: This value ist component specific. You can finetune the component here. Please read the chapter Libraries: Components.
- depends: This array tells the framework on what components this one depends. Depending components will be enabled automatically. Normally you do not have to modify this.
- after: This array tells the framework after of what enabled components this one should be included. Normally you do not have to modify this.
Configuration
Basic Configuration
Some basic configurations you can do here in application/config/master.php. The configuration option are:
- master_doctype: Defines the HTML-Documenttype. Can be one of the following: xhtml11, xhtml1-strict, xhtml1-trans, xhtml1-frame, html5, html4-strict, html4-trans, and html4-frame. Values are saved in the doctypes config file.
- master_author: Set the author.
- master_components: Enable/Disable components.
- master_maintemplate_path: Path to the main template.
- master_components_folder: Path to the folder, where all component views are stored.
- master_static_folder: Path to the folder, where all static views are stored.
And language specific configurations can be done here in language/LANGUAGE/master_lang.php. The configuration options are:
- master_charset: Set charset of the page.
- master_lang: Set the attrinbute "lang" of <html>.
- master_title: Set the <title> of the page.
- master_description: Set the description of the page.
You can read or manipulate all configuration values dynamically from everywhere in your code by accessing the following variables:
- $this->master->config->doctype
- $this->master->config->charset
- $this->master->config->lang
- $this->master->config->title
- $this->master->config->author
- $this->master->config->description
- $this->master->config->maintemplate_path
- $this->master->config->prefix_styles
- $this->master->config->prefix_scripts
- $this->master->config->static_folder
Static Content
Into the following files, located in the folder application/views/master/static, you can add content to include into the master-template statically.
Static content at the beginning of <head>:
- head_init_beforeComponents.php: Before code of components.
- head_init_afterComponents.php: After code of components.
Static content at the end of <head>:
- head_finish_beforeComponents.php: Before code of components.
- head_finish_afterComponents.php: After code of components.
Static content at the beginning of <body>:
- body_init_beforeComponents.php: Before code of components.
- body_init_afterComponents.php: After code of components.
Static content at the end of <body>:
- body_finish_beforeComponents.php: Before code of components.
- body_finish_afterComponents.php: After code of components.
Automatic added Links
Two files will be automatic linked by default:
- Script "global.js"
- Stylesheet "global.css"
You can alter this behaviour by modifying the variables $config['master_autoload_scripts'] and $config['master_autoload_styles'] in the file application/config/master.php. Each of them stores an array with all script- and style-filenames to load.
Class Reference
Enabling/Disabling
You can of course disable the templating completely. This is important for example if writing Ajax-Controllers returning only json or xml. Use the following functions:
- $this->master->disable(): Disable the master-templating.
- $this->master->enable(): Enable the master-templating.
- $this->master->is_enabled(): Returns TRUE if master-templating is enabled.
Alternatively you can create a file named .nomaster in the directory of the controller. The master-template will be disabled for all controllers in this directory.
Add Links to Scripts and Style Sheets
To add a link to a stylesheet-file:
$this->master->add_styleheet($style);To add a link to a javascript-file:
$this->master->add_script($script);- Do not use the extensions ".js" and ".css". They will be appended automatically!
- The parameter will be prefixed by the value of the configuration variable $config['master_prefix_styles'] respectively $config['master_prefix_scripts'] in the file application/config/master.php.
Add Meta Informations
To add a <meta> tag:
$this->master->add_meta($name,$content);Adding any Content by Code
You can use the following functions to add content to the master-template from everywhere in your code:
//Add Content into the head_main Area:
$this->master->add_content_head($content);
//Add Content into the body_main Area - always after the conroller output:
$this->master->add_content_body($content);Advanced Content Adding
You can use the following funtion to inject code into a specific place within the structure:
$this->master->add_content($position, $content);$postion can be one of the following values - which are self-explanatory:
MasterPosition::head_init_beforeComponents_beforeStatic
MasterPosition::head_init_beforeComponents_afterStatic
MasterPosition::head_init_afterComponents_beforeStatic
MasterPosition::head_init_afterComponents_afterStatic
MasterPosition::head_main
MasterPosition::head_finish_beforeComponents_beforeStatic
MasterPosition::head_finish_beforeComponents_afterStatic
MasterPosition::head_finish_afterComponents_beforeStatic
MasterPosition::head_finish_afterComponents_afterStatic
MasterPosition::body_init_beforeComponents_beforeStatic
MasterPosition::body_init_beforeComponents_afterStatic
MasterPosition::body_init_afterComponents_beforeStatic
MasterPosition::body_init_afterComponents_afterStatic
MasterPosition::body_main
MasterPosition::body_finish_beforeComponents_beforeStatic
MasterPosition::body_finish_beforeComponents_afterStatic
MasterPosition::body_finish_afterComponents_beforeStatic
MasterPosition::body_finish_afterComponents_afterStaticDebugging Features
Depending on the variable "ENVIRONMENT" (defined in index.php in the root) the variable $this->master->_DEBUG will be set to TRUE or FALSE. In development environment it is TRUE, otherwise FALSE. You can set this variable everytime to the value you want.
If the variable $this->master->_DEBUG is TRUE, every single section of the master template output will be surrounded by html-comments. Here an example:
<!-- - - - - -body_main_controller - - - - - -->
<h1>Hello World</h1>
<!-- - - - - - - - - - - - - - - - - - - - - -->
Extended Usage
In some cases maybe you have to modify the basic structure of the master template. Or you want to define what else then a html-document. In this case you can modify the file application/views/master/main/main.php. Within the sourcecode of this file you can read all accessible variables.