Page Class
The Page Class provides you with features useful to outputing infomation from PHP to controller views. Examples of these features include asset management, breadcrumb trail creation & PHP to JS variable conversion.
Important: This class is initialized automatically by the system so there is no need to do it manually.
Features:
- Asset Management
- Breadcrumb Trail
- PHP to JS variable conversion
- Meta tag creation
How to use the Page Class?
Page Class is loaded by default by the BackendPro Class. The class file can be found at modules/page/libraries/page.php
Configuration Settings
All configuration settings for the Page Class can be found in modules/page/config/page.php.
| Preference | Default Value | Options | Description |
|---|---|---|---|
| assets_dir | assets/ | None | Location of assets dir |
| admin_assets | assets/admin/ | None | Location of admin assets dir |
| public_assets | assets/public/ | None | Location of public assets dir |
| shared_assets | assets/shared/ | None | Location of shared assets dir |
| default_assets | Array | None | Array of assets to load on every controller. Please see modules/page/config/page.php for futher details. |
| asset_cache_length | 0 | None | Number of hours to keep asset cache's for, 0 means no caching will be used |
| asset_cache_file_pfx | cache_ | None | Asset cache file prefix |
| csstidy_path | plugins/csstidy/class.csstidy.php | None | Relative path from BASEPATH to the main CSS Tidy class file |
| default_page_variables | Array | None | Array of PHP variables to convert into JS variables by default |
Asset Caching
As said before, the page class can provide you with the ability to cache asset files on the server so the client has to open fewer connections to a page, thus decreasing loading time.
Along with caching, optimisation of the contents is also possible. This decreases the overall file size and increases loading speeds further. By default BackendPro comes with the ability to optimise code but requires some steps to turn this feature on
- CSS Optimisation:
CSS optimisation has been implemented by using the CSSTidy library. To get CSS optimisation working please follow these few simple steps.
- Download the latest version of CSSTidy from here (As of writing 1.3)
- In your system/plugins OR system/application/plugins folder extract the files into there own folder. E.g. sysyetm/plugins/csstidy
- Open modules/page/config/page.php and make sure csstidy_path points to class.csstidy.php in your plugins/csstidy folder.
- Turn asset caching on by specifying the asset_cache_length in modules/page/config/page.php.
- JS Optimisation:
Currently not implemented
Dynamic Asset files
It is possible to to use php files to create asset files therefore making use of dynaimc data. If you do want to use a php file as an asset file you must make sure it is formed correctly otherwise when trying to load the page the site will fail.
Example of a PHP CSS asset file
<?php
// Only include header if we are not processing the file for the cache
if( ! isset($cache_output))
header('content-type:text/css');
// Rest of php code to create asset file goes here
?>
As you will see above, the first 4 lines have a condition statement on whether to include information about the file type. What this code does is it only includes the header infomation if the page is not being cached. The reason for this, is when the cache is created it is done by including each file. Trying to change the header part way through this process corrupts the cache and the website. Therefore the need for this check.
Page Class Function Reference
$this->page->set_asset()
Load an asset file:
$this->page->set_asset('shared/admin/public','css/js','filename')
Allows you to specify an asset file to be loaded just for that controller. Please note any assets loaded through this method will not be cached.
The first parameter specifys what type of asset you want to load.
The second parameter is if the asset is a javascript file or a css style sheet.
The third parameter is the filename of the asset, including the extension. Please note you can use .php files since they are executed before inclusion. Please see Asset Caching above for more information.
$this->page->set_variable()
Specify a variable for conversion:
$this->page->set_variable('name',value)
Converts a PHP variable into a javascript variable.
The first parameter is the name you want the javascript variable to be called.
The second parameter is the value you want to convert. You can specify any of the following: string, integer, boolean, null, non-associative nested array.
$this->page->set_crumb()
Create breadcrumb
$this->page->set_crumb('name','link')
Create a new crumb in the breadcrumb trail for this controller. The link should be in the form used by the rest of CodeIgniter, i.e. controller/method
$this->page->set_metatag()
Create meta tag:
$this->page->set_metatag('name','content',TRUE/FALSE)
The first parameter is the name of the tag e.g keywords, description
The second paramter is the content of the meta tag e.g 'text/html; charset=utf-8'
The third optional parameter is a boolean to set whether the tag is the default 'name' or 'http-equiv' version. By default it is set to false ('name' tag type)
$this->page->set_metatag('author','John Doe');
// Creates the following
// <meta name="author" content="John Doe" />
$this->page->set_metatag('content-type','text/html; charset=utf-8',TRUE);
// Creates the following
// <meta http-eqiv="content-type" content="text/html; charset=utf-8" />
It is advised to set base meta tags (i.e. ones to be used throughout the site) in the MY_Controller.php file, in the required classes.
Setting two identical meta tags (i.e the same name) will cause for the second to be displayed, due to it overwriting the initial value.
$this->page->output_variables()
Output javascript variables:
$this->page->output_variables(TRUE/FALSE)
The first optional parameter specifys if the HTML should be printed or returned. The default behavior is true (boolean), the output will be printed, if set to false (boolean) the output will be returned.
$this->page->output_metatags()
Output meta tags:
$this->page->output_metatags(TRUE/FALSE)
The first optional parameter specifys if the HTML should be printed or returned. The default behavior is true (boolean), the output will be printed, if set to false (boolean) the output will be returned.
$this->page->output_trail()
Output breadcrumb trail:
$this->page->output_trail(TRUE/FALSE)
Creates breadcrumb trail HTML. An example of the code created:
<div id="breadcrumb">
<a href="first-link" title="First Crumb">First Crumb</a> »
<a href="second-link" title="Second Crumb">Second Crumb</a> »
Third Crumb
</div>
The first optional parameter specifys if the HTML should be printed or returned. The default behavior is true (boolean), the output will be printed, if set to false (boolean) the output will be returned.
$this->page->output_assets()
Output assets:
$this->page->output_assets('admin/public',TRUE/FALSE)
Creates HTML code to include asset files into view file. An example of the code created:
<script type="text/javascript" src="http://localhost/backendpro/assets/shared/js/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="http://localhost/backendpro/assets/shared/css/reset.css" />
<link rel="stylesheet" type="text/css" href="http://localhost/backendpro/assets/shared/css/typography.css" />
<link rel="stylesheet" type="text/css" href="http://localhost/backendpro/assets/admin/css/style.css" />
The first paramter specifies which asset area should be outputed. This allows either the public or administration assets to be choosen between.
The second optional parameter specifys if the HTML should be printed or returned. The default behavior is true (boolean), the output will be printed, if set to false (boolean) the output will be returned.
Important: Assets are outputed to the page in a specific order.
Shared Javascript Files
Shared CSS Files
Shared Conditional CSS Files
THEN EITHER
Admin Javascript Files
Admin CSS Files
Admin Conditional CSS Files
OR
Public Javascript Files
Public CSS Files
Public Conditional CSS Files
Depending on what string you specify as the first parameter to the function call.
$this->page->icon()
Display Icon
$this->page->icon('name')
Return an <image> tag to display the requested icon. Where name can be any icon file in assets/shared/icons excluding the .png file extension.