Template Injector a templating system for CodeIgnitor
Template Injector is an open source library class that I have written for the CodeIgniter PHP framework. It makes it simple and clean for you to keep your site-consistent html (header, footer, navigation menu etc.) in a single template view file and then dynamically inject your page specific content into that template at runtime. Template Injector includes methods to dynamically alter key variables in your template file - page title, page ID and links to external CSS and script files from within your controller.
Feel free to get in touch if you have any questions or comments.
Download
Requirements
- PHP 5+
- CodeIgniter 2+
Setup
- Create a template file or use the example one included with the download package and place this in the "views" folder of your application. I find it convenient to keep my one or two template files in their own folder called "templates".
- Place template_injector.php in the "libraries" file of your Code Ignitor application
- Set "$templates_dir" and "$template" on lines 78 and 80 of template.php to point towards your template folder.
- Alter the default variables in template_injector.php to suit your application (see bellow).
Load Library
Set config/autoload.php to autoload 'template_injector' or, if you wish to load from the controller use:
$this->library->load('template_injector');
To Use
At it's simplest, Template Injector needs just one line of code in your controller, very similar to loading a view:
$this->template_injector->load('demo_content.php', $data);
The second, optional parameter (array) propagates your content and template files just as though you are using a $this->load->view().
Further parameters can be set by class methods...
Set Page Title
Optional. A Page Title variable can be set with:
$this->template_injector->set_title('My Page Title');
$this->template_injector->load('demo_content');
In order for the Page Title to display in your template
$TI_page_title
needs to be added to the appropriate part of your template file. See the example files.
Set Page ID
Optional. A Page ID variable can be set with:
$this->template_injector->set_id('my-page-id');
$this->template_injector->load('page_content');
In order for the Page Title to display in your template
$TI_page_id
needs to be added to the appropriate part of your template file. See the example files.
Set Script File
Optional. A Page ID variable can be set with:
$this->template_injector->set_script('my_script.js');
In order for the script link to display in your template
$TI_file_includes
needs to be added to the header of your template file. Note that this variable only needs to be placed in your template file once even if you have set multiple CSS and / or script files.
Set CSS File
Optional. A Page ID variable can be set with:
$this->template_injector->set_css('my_style.css');
In order for the script link to display in your template
$TI_file_includes
needs to be added to the header of your template file. Note that this variable only needs to be placed in your template file once even if you have set multiple CSS and / or script files.
The set_css() and set_script() both take an optional second parameter. The default value is TRUE and sets the
Set Alternative Template File
Optional. Most of my websites use a single template file that I set in the template_injector.php vars but occasionally it useful t be able to load a different layout.
$this->template_injector->set_template('demo_alternative_template.php');
In order for the script link to display in your template
$TI_file_includes
needs to be added to the header of your template file. Note that this variable only needs to be placed in your template file once even if you have set multiple CSS and / or script files.
Application Wide Config Variables
Set in template_injector.php.
- template folder (within your "views" folder)
- template view file
- page title seperator
Default CSS and Script files can be set by calling:
$this->set_css('defaule_style.css');
and
$this->set_script('defaule_script.js');
from within the __construct() function of Template_injector in template_injector.php