Giter Site home page Giter Site logo

codeigniter-assets-helper's Introduction

CodeIgniter-Assets-Helper

Helper to load assets (images, stylesheets or javascript).

Requirements

  • CodeIgniter should be configured to load url_helper automatically.

Installation

Copy assets_helper.php into the applications/helpers directory and copy assets.php into the applications/config directory.

Configuration

You need to set your assets folder for each type (images, stylesheets or javascript). See applications/config/assets.php for more information.

Usage

One file without custom attributes:

assets_css('example.css');

# Results in
<link rel="stylesheet" type="text/css" href="http://www.website.com/assets/css/example.css">
assets_js('example.js');

# Results in
<script type="text/javascript" src="http://www.website.com/assets/js/example.js"></script>
assets_img('example.png');

# Results in
<img src="http://www.website.com/assets/img/example.png"/>

One file with optional custom attributes

assets_css('example.css', array('media' => 'screen'));
# Results in
<link rel="stylesheet" type="text/css" href="http://www.website.com/assets/css/example.css" media="screen">
assets_js('example.js', array('charset' => 'utf-8'));
# Results in
<script type="text/javascript" src="http://www.website.com/assets/js/example.js" charset="utf-8"></script>
assets_img('example.png', array('title' => 'Awesome Helper'));
# Results in
<img src="http://www.website.com/assets/img/example.png" title="Awesome Helper"/>

Multiple files, with or without custom attributes

$multiple_css = array('example.css', 'style.css');
assets_css($multiple_css); 
# Results in
<link rel="stylesheet" type="text/css" href="http://www.website.com/assets/css/example.css">
<link rel="stylesheet" type="text/css" href="http://www.website.com/assets/css/style.css">
$multiple_js = array('example.js', array('style.js', array('charset' => 'utf-8')));
assets_js($multiple_js);
# Results in
<script type="text/javascript" src="http://www.website.com/assets/js/example.js"></script>
<script type="text/javascript" src="http://www.website.com/assets/js/style.js" charset="utf-8"></script>
$multiple_img = array(array('example.png', array('alt' => 'ranisalt rules')), array('style.css', array('title' => 'Awesome Helper')));
assets_img($multiple_img);
# Results in
<img src="http://www.website.com/assets/img/example.png" alt="ranisalt rules"/>
<img src="http://www.website.com/assets/img/style.css" title="Awesome Helper"/>

You probably got the hang of it.

codeigniter-assets-helper's People

Contributors

datamweb avatar gpedro avatar rambhosale avatar ranisalt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

codeigniter-assets-helper's Issues

Adding video in asset

Hello, am trying to expound on this lib to accommodate video but its not working for me. what am I doing wrong?

Assets.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['path_base'] 		    = 'assets';
$config['path_js'] 		    = 'js';
$config['path_css'] 	    = 'css';
$config['path_img'] 		= 'img';
$config['path_vid'] 		= 'video';

assets_helper.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

define('DS', DIRECTORY_SEPARATOR);


if (!function_exists('_getConfig'))
{
  function _getConfig()
  {
    $CI =& get_instance();
    $CI->load->config('assets');
    $config = array();
    $config['path_base'] = $CI->config->item('path_base');
    $config['path_js']   = $CI->config->item('path_js');
    $config['path_css']  = $CI->config->item('path_css');
    $config['path_img']  = $CI->config->item('path_img');
    $config['path_vid']  = $CI->config->item('path_vid');
    
    return $config;
  }
}

if (!function_exists('_process_array'))
{
  function _process_array($data, $type)
  {
    if(is_array($data))
    {
      $head = '';
      $attr = '';
      foreach($data as $parent)
      {
        if(is_array($parent))
        {
          foreach($parent as $child_1_key => $child_1_value)
          {
            if(is_array($child_1_value))
            {
              $attr .= ' ';
              foreach($child_1_value as $child_2_key => $child_2_value)
              {
                $attr .= $child_2_key.'="'.$child_2_value.'"';
              }
            }
            else
            {
              $file = $child_1_value;
            }
          }
        } else {
          $file = $parent;
        }
      
        $config = _getConfig();
        $path = base_url($config['path_base'].DS.$config['path_'.$type].DS.$file);
      
        if($type == 'js')
          $head .= '<script type="text/javascript" src="' . $path . '"' . $attr . '></script>';
        else if($type == 'css')
          $head .= '<link rel="stylesheet" type="text/css" href="' . $path . '"' . $attr . '>';
        else if($type == 'img')
          $head .= '<img src="' . $path . '"'.$attr.'/>';
        else if($type == 'vid')
          $head .= '<source type="video/mp4 src="' . $path . '"'.$attr.'>';
      } 

      return $head;
    }
  }
}

if (!function_exists('_assets_base'))
{
  function _assets_base($file, $attr ,$type)
  {
    if(is_array($file))
    {
      return _process_array($file, $type);
    }
    else
    {
      $attribute = ' ';
      if(!empty($attr) && is_array($attr))
      {
        foreach($attr as $key => $value)
        {
          $attribute .= ' '.$key.'="'.$value.'"';
        }
      }
    
      $config = _getConfig();
      $path = base_url($config['path_base'].DS.$config['path_'.$type].DS.$file);
    
      if($type == 'js')
        return '<script type="text/javascript" src="' . $path . '"' . $attribute . '></script>';
      else if($type == 'css')
        return '<link rel="stylesheet" type="text/css" href="' . $path . '"' . $attribute . '>';
      else if($type == 'img')
        return '<img src="' . $path . '"'.$attribute.'/>';
    }
  }
}

if (!function_exists('assets_css'))
{
  function assets_css($file, $attr = array())
  {
    return _assets_base($file, $attr, 'css');
  }
}

if (!function_exists('assets_js'))
{
  function assets_js($file, $attr = array())
  {
    return _assets_base($file, $attr, 'js');
  }
}

if (!function_exists('assets_img'))
{
  function assets_img($file, $attr = array())
  {
    return _assets_base($file, $attr, 'img');
  }
}

if (!function_exists('assets_vid'))
{
  function assets_vid($file, $attr = array())
  {
    return _assets_base($file, $attr, 'vid');
  }
}

calling on my view with echo assets_vid('video.mp4')

Looking forward to here from you. Thanks.

Documentation issue

I'm not sure if this was intended but in the Usage section, more specifically under the One file with optional custom attributes paragraph in all the three examples the closing brackets and the semicolons are missing.

assets_css('example.css', array('media' => 'screen')
should be
assets_css('example.css', array('media' => 'screen'));

and so on. Same issue but only with the semicolons in the section above.

assets.php

This worked for me:
echo assets_css($multiple_css);

$config = array(

'path_base' => 'assets',
'path_js'   => 'js',
'path_css'  => 'css',
'path_img'  => 'img',

);

PHP Error $attribute

to avoid error please fix
==>
in assets_helper.php
in function _assets_base($file, $attr ,$type)
from
else { if(!empty($attr) && is_array($attr)) { $attribute = ' '; foreach($attr as $key => $value) { $attribute .= ' '.$key.'="'.$value.'"'; } }

to

else { $attribute = ' '; if(!empty($attr) && is_array($attr)) { foreach($attr as $key => $value) { $attribute .= ' '.$key.'="'.$value.'"'; } }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.