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

[eluser]Nanodeath[/eluser]
Hi all,

I've created a little MY_Loader class that does some nifty stuff that I'll share if anyone wants it. As for what it does, I'll just cut to the chase.

It affects loading views only. If you load a view "my_view", then it automatically checks to see if there's a "my_view.js" and a "my_view.css" in a location of your choice and loads those if found. Additionally, there's a "dependencies" file in which you can specify what additional css and js files should be loaded for specific views or all views in your application. These files can be loaded every time or conditionally. For example, here's my dependencies file:

Code:
$dep['all']['js'][] = "../jquery.js";
$dep['all']['js'][] = "../interface.js";
$dep['all']['js'][] = array("../jquery.simplemodal.js", '!$CI->session->userdata(\'username\')');
$dep['all']['js'][] = array("login_modal.js", '!$CI->session->userdata(\'username\')');

$dep['learn_index']['js'][] = '../ui.tabs.js';
$dep['learn_index']['css'][] = 'http://dev.jquery.com/view/trunk/themes/flora/flora.all.css';

All views load jquery.js and interface.js in my case, and if the user is not logged in, the modal js files are loaded. For the learn_index view, learn_index.js and learn_index.css are loaded, along with ui.tabs.js and that flora.all.css.

I'm exploring a couple other ideas, like allowing the dev to pass variables to the load->view with a special prefix that can be used in the conditions in the dependencies file, or making a separate file that consolidates as much external javascript as is feasible into one dynamic file.

Anyone want this when I'm done with it, have comments, ideas, etc..? It's meant to just be a lightweight little helper. I like it because if I want to create custom javascript for one of my views, I don't have to modify the view, just create a file.
#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.
#3

[eluser]Nanodeath[/eluser]
Hmm...that does seem like a good elegant idea, but what I'm really looking for is a mechanism that doesn't require you to modify the views, rather just a central location where you can factor out the files that are common to all views, along with conditional inclusion of scripts. But, what you have would also work, too.




Theme © iAndrew 2016 - Forum software by © MyBB