Skip to content

Instantly share code, notes, and snippets.

@jcc2010
Created April 18, 2011 23:53
Show Gist options
  • Save jcc2010/926549 to your computer and use it in GitHub Desktop.
Save jcc2010/926549 to your computer and use it in GitHub Desktop.
A quick assets file to give you control over header and footer templates with codeigniter. Not fully documented (yet) but should be relatively easy to figure out how to get access from your controller to tpl.view files.
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Assets
{
var $ci;
var $siteTitle;
var $siteJSPath;
var $siteCSSPath;
var $siteImgPath;
var $siteSeperator;
/**
* Constructor
*
*/
public function __construct()
{
$this->ci =& get_instance();
$this->ci->config->load('assets');
$this->siteTitle = $this->ci->config->item('siteTitle');
$this->siteJSPath = $this->ci->config->item('siteJSPath');
$this->siteCSSPath = $this->ci->config->item('siteCSSPath');
$this->siteImgPath = $this->ci->config->item('siteImgPath');
$this->siteSeperator = $this->ci->config->item('siteSeperator');
}
/**
* Add Script links to output array
*
* @access public
* @global array assetsScript
* @param string $position Either 'head' or 'body'
* @param string $filename filename
* @param string $type Application type of script. Default text/javascript.
* @param bool $async Defines if the script should be executed asynchronously or not
* @param bool $defer Indicates that the script is not going to generate any document content.
* @return bool
*/
function addScript($position, $filename, $type = false, $async = false, $defer = false)
{
if (empty($position) || empty($filename)) {
// Fail silently Intentionally. Could extend with PHP5 Exception.
return false;
}
if (!isset($GLOBALS['assetsScript'])) {
$GLOBALS['assetsScript'] = array('head'=>array(), 'body'=>array());
}
$GLOBALS['assetsScript'][$position][] = array('filename' => $filename,
'type' => $type,
'async' => $async,
'defer' => $defer);
return true;
}
/**
* Add Script text to document head or body
*
* @access public
* @global array assetsScript
* @param string $script script to be added
* @param string $position Either 'head' or 'body'
* @return bool
*/
function addInlineScript($script, $position = 'head')
{
if (!isset($GLOBALS['assetsInlineScript'])) {
$GLOBALS['assetsInlineScript'] = array('head'=>array(), 'body'=>array());
}
$GLOBALS['assetsInlineScript'][$position][] = array('script' => $script);
return true;
}
function addJavascript($position, $path)
{
return $this->addScript($position, $path);
}
/**
* Add Link to output array
*
* @access public
* @global array assetsLink
* @param string $filename filename
* @param string $media Specifies on what device the document will be displayed.
* @param string $type Specifies the MIME type of the target URL.
* @param string $rel Specifies the relationship between the current document and the target URL.
* @param string $sizes Defines sizes of the linked resource.
* @param string $hreflang Defines the base language of the target URL.
* @return bool
*/
function addLink($filename, $media = false, $type = false, $rel = false, $sizes = false, $hreflang = false)
{
if (empty($filename)) {
// Fail silently Intentionally. Could extend with PHP5 Exception.
return false;
}
if (!$media){
$media = 'screen';
}
if (!$rel){
$rel = 'stylesheet';
}
if (!$type){
$type = 'text/css';
}
if (!isset($GLOBALS['assetsLink'])) {
$GLOBALS['assetsLink'] = array();
}
$GLOBALS['assetsLink'][] = array('filename' => $filename,
'media' => $media,
'type' => $type,
'rel' => $rel,
'sizes' => $sizes,
'hreflang' => $hreflang);
return true;
}
/**
* Add Meta to output array
*
* @access public
* @global array assetsMeta
* @param string $name Connects the content attribute to a name.
* @param string $content Defines meta information to be associated with http-equiv or name
* @param string $httpequiv Connects the content attribute to an HTTP header
* @param string $charset Defines the character encoding for the document
* @return bool
*/
function addMeta($name, $content, $httpequiv = false, $charset = false)
{
if (empty($name) || empty($content)) {
// Fail silently Intentionally. Could extend with PHP5 Exception.
return false;
}
if (!isset($GLOBALS['assetsMeta'])) {
$GLOBALS['assetsMeta'] = array();
}
$GLOBALS['assetsMeta'][] = array('name' => $name,
'content' => $content,
'httpequiv'=> $httpequiv,
'charset' => $charset);
return true;
}
/**
* Add Title to output array
*
* @access public
* @global array assetsTitle
* @param string $title Page Title.
* @return bool
*/
function addTitle($title)
{
if (empty($title)) {
return false;
}
if (!isset($GLOBALS['assetsTitle'])) {
$GLOBALS['assetsTitle'] = array();
}
$GLOBALS['assetsTitle'][] = array('title' => $title);
return true;
}
function addPageTitle($title)
{
return $this->addTitle($title);
}
function renderHeadScript()
{
if (!isset($GLOBALS['assetsScript'])) {
return false;
}
foreach ($GLOBALS['assetsScript'] as $key => $var) {
if ($key == 'head'){
foreach ($var as $data){
echo '<script src="'. base_url() . $this->siteJSPath .'/'. $data['filename'] . '"></script>' . "\n";
}
}
}
return false;
}
function renderHeadInlineScript()
{
if (!isset($GLOBALS['assetsInlineScript'])) {
return false;
}
foreach ($GLOBALS['assetsInlineScript'] as $key => $var) {
if ($key == 'head'){
foreach ($var as $data){
echo $data['script'] . "\n";
}
}
}
return false;
}
function renderHeadJavaScript()
{
return $this->renderHeadScript();
}
function renderBodyJavaScript()
{
return $this->renderBodyScript();
}
function renderBodyScript()
{
if (!isset($GLOBALS['assetsScript'])) {
return false;
}
foreach ($GLOBALS['assetsScript'] as $key => $var) {
if ($key == 'body'){
foreach ($var as $data){
echo '<script src="
'. base_url() . $this->siteJSPath . '/' . $data['filename'] . '"></script>' . "\n";
}
}
}
return false;
}
function renderBodyInlineScript()
{
if (!isset($GLOBALS['assetsInlineScript'])) {
return false;
}
foreach ($GLOBALS['assetsInlineScript'] as $key => $var) {
if ($key == 'body'){
foreach ($var as $data){
echo $data['script'] . "\n";
}
}
}
return false;
}
function renderCSS()
{
if (!isset($GLOBALS['assetsLink'])) {
return false;
}
foreach ($GLOBALS['assetsLink'] as $var) {
echo '<link type="text/css" rel="stylesheet" media="' . $var['media'] .'" href="'. base_url() .'assets/css/' . $var['filename'] . '" />' . "\n";
}
return false;
}
function renderTitle()
{
if (!isset($GLOBALS['assetsTitle'])) {
return $this->siteTitle;
}
foreach ($GLOBALS['assetsTitle'] as $var) {
return $this->siteTitle .' : '. $var['title'];
}
return false;
}
function renderPageTitle()
{
if (!isset($GLOBALS['assetsTitle'])) {
echo $this->siteTitle;
return;
}
foreach ($GLOBALS['assetsTitle'] as $var) {
echo $var['title'];
}
return false;
}
function renderSiteTitle()
{
echo $this->siteTitle;
return false;
}
private function getBaseUrl()
{
$this->CI = &get_instance();
return $CI->config->item('base_url');
}
private function getSiteTitle()
{
$this->CI = &get_instance();
return $CI->config->item('base_url');
}
}
?>
@jcc2010
Copy link
Author

jcc2010 commented Apr 18, 2011

Somewhat based on the similar xaraya functions. GPL, feel free to share.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment