ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

External v2 - Route Based Asset Management

February 17, 2011 2:09pm

Subscribe [5]
  • #1 / Feb 17, 2011 2:09pm

    darkhouse

    323 posts

    Greetings! I’ve been off the forums for a while, but I’m back, with a vengeance. I recently rewrote my External library (see previous version).

    Here is the link to the new library. https://github.com/darkhouse/External

    There were a lot of things I wanted to incorporate into it so I’ve rewritten it from scratch. Version 2 is based on the same principle that you can specify assets for certain routes, but with some new, I think cool, features. I’ve explained them in more detail on the wiki, but here’s a summary.

    Groups - Everything is group based now (inspired by tonydewan’s Carabiner) so you can load only assets from a specific group if you want. You can also load assets in more than one place on a page, so you can target different assets for different parts of your page.

    Inheritance! - You can specify assets for a route, but also inherit assets from another route.

    Exclusions - when inheriting, you can tell it to ignore certain groups.

    Weights - you can load the assets in whatever order you want.

    LESS.js - I use LESS.js myself, so I’ve added support for that.

    Pretty Output - There is a tabs param so that the output matches the indentation of the rest of your code.

    There are more details on the wiki, and some usage code. Also, I’ve added a sample config file that has the settings I used for a site recently. That should give you a pretty good idea how to setup your config file.

    Thanks to everyone who used my original, and I hope this new version helps make things even better.

  • #2 / Feb 21, 2011 10:56pm

    jarthur

    2 posts

    Not sure if i’m doing something wrong, but on PHP 5.3 i get the following error/warning:

    Severity: Warning
    Message: Parameter 1 to array_multisort() expected to be a reference, value given
    Filename: libraries/External.php
    Line Number: 325

    I think it is related to this: http://bugs.php.net/bug.php?id=43568

  • #3 / Feb 21, 2011 11:19pm

    jarthur

    2 posts

    This code change worked for me (starting on line 311 of the External.php file):

    private function _multisort(){
            $args = func_get_args();
            $data = array_shift($args);
                    if (!is_array($data))
                        return false;
    
                    $multisort_params = array();
            foreach ($args as $n => $field) {
                if (is_string($field)) {
                    ${"tmp_$n"} = array();
                    foreach ($data as $key => $row){
                        ${"tmp_$n"}[$key] = $row[$field];
                    }
                    $multisort_params[$n] = &${"tmp_$n"};
                }
            }
            $multisort_params[] = &$data;
            call_user_func_array('array_multisort',$multisort_params);
            return array_pop($multisort_params);
        }
  • #4 / Feb 22, 2011 12:26am

    darkhouse

    323 posts

    Thanks, I’ll make this change - I grabbed that multisort code from php.net, I didn’t modify it, and it was working fine for me on v5.3.5

  • #5 / Feb 22, 2011 2:20am

    darkhouse

    323 posts

    I’ve changed this, and modified the cachebusting system based on CroNiX’s suggestion. Thanks guys. I’ve also added some better documentation, added a markdown readme file which goes over the config file in pretty good detail (I think). As always, if anyone has any questions, please feel free to ask.  Thanks!

  • #6 / Jul 17, 2011 8:39pm

    dchankhour

    6 posts

    I have issues with sub pages.  i have a controller search/advanced with the url http://mydomain.com/search/advanced but the css is being loaded as follow

    search/advanced/includes/styles.css

    where the link should be just

    includes/styles.css

    any thoughts on this one?

    Thanks

  • #7 / Jul 17, 2011 11:47pm

    darkhouse

    323 posts

    dchankhour: are you using a <base> tag?

  • #8 / Jul 19, 2011 1:24am

    icomefromthenet

    22 posts

    might just be use this in my next project, beats hard-coding in controllers thanks for posting.

  • #9 / Jul 21, 2011 3:45am

    dchankhour

    6 posts

    Sorry darkhouse, i’m not sure what you meant by <base>. 

    I basically have my site in a subfolder.  in the site i have a search folder and i have home.php and an advanced.php controller in the search folder.  when i view the /search/home every thing works fine but when i view search/advanced it does not load any css files.

    it basically try loading http://localhost/mysite/search/includes/styles.css

    instead of http://localhost/mystie/includes/styles.css

    thanks for your assistance.

  • #10 / Jul 21, 2011 9:49am

    darkhouse

    323 posts

    dchankhour: Right, you need a <base> tag. Put this in your <head>

    <base href="<?php echo base_url(); ?>"></base>

    If you’re not already loading the url helper, I would autoload it. Just add ‘url’ to the helper array in application/config/autoload.php and then base_url() will work.

  • #11 / Jul 23, 2011 6:06pm

    dchankhour

    6 posts

    Thanks a lot.

    That worked.

  • #12 / Jul 27, 2011 9:31am

    quasiperfect

    132 posts

    hi

    i set external and external config to autoload
    when i try to use $this->external->get(‘admin_login’, ‘head’, false, 4);
    i get

    A PHP Error was encountered
    Severity: Warning
    Message: Missing argument 1 for CI_External::__construct(), called in D:\server\www\qpshop\system\core\Loader.php on line 996 and defined
    Filename: libraries/external.php
    Line Number: 26

    this is my config file

    <?php if(!defined('BASEPATH')) exit('No direct script access allowed');
    $config['routes'] = array(
        'all' => array(
            'css' => array(
                array('data' => 'assets/css/reset.css', 'group' => 'head', 'weight' => 1),
                array('data' => 'assets/css/final.css', 'group' => 'head', 'weight' => 999),
            ),
            'js' => array(
                array('data' => 'assets/js/libs/less.js', 'group' => 'head', 'weight' => 10),
                array('data' => 'assets/js/libs/modernizr.js', 'group' => 'head', 'weight' => 10),
                array('data' => 'assets/js/libs/jquery.js', 'group' => 'foot', 'weight' => 1),
                array('data' => 'assets/js/libs/apprise.js', 'group' => 'foot', 'weight' => 2),
                array('data' => 'assets/js/libs/fancybox.js', 'group' => 'foot', 'weight' => 3),
            )
        ),
        'admin' => array(
            'css' => array(
                array('data' => 'assets/css/admin/common.less', 'type' => 'less', 'group' => 'head', 'weight' => 1),
            ),
            'inherit' => array(
                'parent' => array('assets' => 'all')
            )
        ),
        'admin_login' => array(
            'css' => array(
                array('data' => 'assets/css/admin/login.less', 'type' => 'less', 'group' => 'head', 'weight' => 1),
            ),
            'inherit' => array(
                'parent' => array('assets' => 'admin')
            )
        )
    );
    
    $config['default_cache'] = true;

    what i’m doing wrong ?

    Edit 1
    if i don’t autoload the config file i don’t get any error but no results

    my folder structure is
    -application
    -assets
    —css
    —js
    -system

    Edit2
    if i call $this->external->get(‘all’, ‘head’, false, 4); it works

    so i think i don’t understand how inheritance works
    if i call the route admin_login i want to get admin and from admin it should get all
    but i get nothing

    Edit3
    ok now i got it by route you really mean route like in controller/function/
    but i have a new problem i use languge uri identifier
    so my urls are like this site.com/language/controller/function
    how can i make external to ignore the language ?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases