Welcome Guest, Not a member yet? Register   Sign In
Autoloading external files - extended view library
#2

[eluser]wiredesignz[/eluser]
There are probably nicer ways to do this without extending CI_Loader.

You could use an asset helper in your view which receives your dependency array and builds the links accordingly.

Example: application/helpers/asset_helper.php
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

    function asset($type = 'css', $assets = array(), $url = NULL, $crlf = "\n")
    {
        $str = '';
        
        foreach ($assets as $source => $attrib)
        {
            $href = ($url) ? $url : base_url();
        
            $source = $href . str_replace(".{$type}", '', $source);
        
            switch ($type)
            {            
                case 'css':
                {
                    $str .= '<link rel="stylesheet" type="text/css" href="'.$source.'.'.$type.'" '.$attrib.' />'."\n";
                    break;
                }
                case 'js':
                {
                    $str .= ''."\n";
                    break;
                }
                case 'img':
                {
                    $str .= '<img src="'.$source.'" '.$attrib.' />'."\n";
                    break;
                }
            }
        }
        return $str;
    }
    
    function meta_tag($type, $content = array(), $crlf = "\n")
    {
        $str = '';
        foreach ($content as $key => $value)
        {
            switch ($type)
            {
                case 'http':
                {
                    $str .= '&lt;meta http-equiv="'.$key.'" content="'.$value.'" /&gt;'.$crlf;
                    break;
                }
                case 'name':
                {
                    $str .= '&lt;meta name="'.$key.'" content="'.$value.'" /&gt;'.$crlf;
                    break;
                }
            }
        }
        return $str;
    }
Note: the forums always break the code in the javascript part of the helper


Usage:
Code:
&lt;?php    
            //uses asset_helper
            echo meta_tag('http', array(
                'content-type'     => 'text/html; charset=UTF-8',
            ));
            
            echo meta_tag('name', array(
                'author'         => 'Wiredesignz, <[email protected]>',
                'robots'         => 'index, follow',
                'description'     => $title,
            ));
            
            echo asset('css', array(
                'css/shop'     => 'media="screen"',
                'css/login'    => 'media="screen"',
            ));
            
            echo asset('js', array('js/shop_functions' => '',));
?&gt;

The assets array could be passed into your view quite easily.


Messages In This Thread
Autoloading external files - extended view library - by El Forum - 02-08-2008, 10:30 PM
Autoloading external files - extended view library - by El Forum - 02-08-2008, 10:39 PM
Autoloading external files - extended view library - by El Forum - 02-08-2008, 11:28 PM



Theme © iAndrew 2016 - Forum software by © MyBB