Welcome Guest, Not a member yet? Register   Sign In
BackendPro 0.6.1

[eluser]adamp1[/eluser]
This is my problem at the moment. So currently its implemented to cache but browser specific rules don't work. Unless I can find a solution for both then I will most likely get rid of the server caching and make sure browser specific stylesheets work (as in your code).

Like you say I'm not happy about hacks, I have to admit when I first wrote the asset library for BackendPro I didn't know the comments where HTML only (thought they were CSS).

So as soon as exams are over I most likely will release a browser specific version.

Thank you for the work on it btw, hope your finding the application as a whole helpfull.

[eluser]ddrury[/eluser]
Adam,

Thinking about how you've currently implemented the file caching, I think there is a fundamental problem. If you're amalgamating a number of files then surely the browser's html should only have one include line for the amalgamated .js or .css file but currently there is a line generated for each 'original' file so even if a server cache is built I don't think it'll be used.

Have you seen this site http://www.ejeliot.com/blog/72.

Further thinking; should you get this sorted is there not some way to make the ie conditional files avoid the cache altogether?

Thus you'd end up with something like this in the browser
Code:
<link rel="stylesheet" type="text/css" href="combinedstuff.css">
..
..
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="uncachedstuff.css">
<![endif]--&gt;

Dave

[eluser]adamp1[/eluser]
You mustn't have turned server caching on. When it is on there is only 1 include line for css and js.

I will have to look into the browser specific styles as said (as said before it as to wait a week or so)

[eluser]ddrury[/eluser]
Hi Adam,

You were right - caching was off.

Further thoughts on the css files, howabout creating another array in modules\page\config\page.php something like

$config['default_assets'] = array(admin(..),public(..),shared(css(..),js(..),conditional(..))

to hold all these conditional files (named along the lines I suggested earlier) and then either just load them wrapped in a conditional comment with set_asset or (thinking aloud) use the user_agent class to check them against the current browser and only load as required.

Either way they won't be loaded in the main cache.

Dave

[eluser]ddrury[/eluser]
Right!

I think this works and may form the basis for the way forward.
In modules\page\config\page.php
create new arrays 'cond_css' for each of the admin,public & shared areas.

In modules\page\libraries\Page.php.
modify function output_assets() to handle 'cond_css' (2 lines marked with comment)
create new function _include_cond_css to handle the output.

BTW in the release version of the code you call for navigation.css in the admin area but it's missing!

Dave

Code:
$config['default_assets'] = array(
    'admin'  => array(
        'css'   => array('layout.css','style.css'),
        'js'    => array('navigation.js','select_all.js'),
        'cond_css'  => array('ie.ie.css','gte_ie_6.ie.css','ie_5.ie.css','ie_6.ie.css')),
    'public' => array(
        'css'   => array('layout.css'),
        'js'    => array(),
        'cond_css'  => array('ie.ie.css','gte_ie_6.ie.css','ie_5.ie.css','ie_6.ie.css')),
    'shared' => array(
        'css'   => array('reset.css','typography.css','forms.css','FlashStatus.css','icons.php','treeview.css'),
        'js'    => array('jquery.js','interface.js','jquery.cookie.js','jquery.treeview.js'),
        'cond_css'  => array()),
);


        function output_assets ($area = 'public',$print = TRUE)
        {
            if ($area != 'public' AND $area != 'admin')
            {
                // Just check a valid area has been given
                log_message("error","Cannot link asset area '" . $area . "'.");
                return;
            }
            // PREPARE VARIABLE OUTPUT
            // Transfer PHP variables into JS variables
            if (count($this->variables) != 0)
            {
                $this->output .= "\n&lt;!--\n";
                foreach($this->variables as $name => $value)
                {
                    $this->output .= "var " . $name . " = ";
                    $this->_handle_variable($value);
                    $this->output .= ";\n";
                }
                $this->output .= "// --&gt;\n\n";
            }

            // PREPARE ASSET OUTPUT
            $this->CI->load->helper('file');
            foreach(array('shared',$area) as $type)
            //foreach(array($area) as $type)
            {
                foreach(array('css','js','cond_css') as $asset) // CHANGED LINE
                {
                    // Asset path
                    $dir = $this->CI->config->item($type . "_assets") . $asset . "/";

                    // Get all files in asset path
                    $asset_files = get_filenames($dir);

                    // First lets check if there is a cache
                    if($this->CI->config->item('asset_cache_length') != 0 && $asset != 'cond_css') //CHANGED LINE
                    {
                        $is_cache = FALSE;
                        foreach($asset_files as $file)
                        {
                            if( preg_match("/".$this->CI->config->item('asset_cache_file_pfx')."([0-9]+).*/",$file,$match)){
                                // Check if the cache file has not expired
                                if($match[1] >= time())
                                {
                                    // Cache is valid
                                    $is_cache = TRUE;
                                    $this->{'_include_' . $asset}($dir . $file);
                                }
                                else
                                {
                                    // Remove old cache file
                                    unlink($dir . $file);
                                }
                                break;
                            }
                        }

                        // We couldn't find a valid cache file so create one
                        if( ! $is_cache)
                            $this->_write_cache($dir,$type,$asset);
                    }
                    else
                    {
                        // Caching is not used link files normally
                        foreach($this->default_assets[$type][$asset] as $file)
                        {
                            if( file_exists ($dir . $file))
                            {
                                $this->{'_include_' . $asset}($dir . $file);
                            }
                        }
                    }

                    // Link any extra asset files loaded on the fly
                    if( isset($this->extra_assets[$type][$asset]) && count($this->extra_assets[$type][$asset]) != 0)
                    {
                        foreach($this->extra_assets[$type][$asset] as $file)
                        {
                            $this->{'_include_' . $asset}($dir . $file);
                        }
                    }
                }
            }

            // Output HTML
            if($print){
                print $this->output;
            } else {
                return $this->output;
            }
        }
        function _include_cond_css($file)
        {
          if (strpos($file,'.ie.css')===FALSE)
          {
              $prefix = $suffix = '';
          }
          else
          {
            $prefix = '&lt;!--[if ' . preg_replace(array('/ie/','/_/'),array('IE',' '),basename($file,'.ie.css')) . ']>';
            $suffix = '<![endif]--&gt;';
          }
          $this->output .= $prefix . '&lt;link rel="stylesheet" type="text/css" href="' . base_url() . $file . '" /&gt;' . $suffix;
          $this->output .= "\n";
        }

[eluser]adamp1[/eluser]
Ill have a look at this soonish, thanks for the input.

[eluser]ddrury[/eluser]
Hi Adam,

Hope the finals are going (have gone?) well!

I've been thinking further about this asset loading lark. My latest changes work and allow conditional loading of css (and potentially javascript), however, because of the loading sequence the Page class uses (e.g shared-js, shared-css, shared-cond_css followed by admin-js, admin-css, admin-cond_css) the shared-cond_css virtually becomes redundant because of the likelyhood of being overwritten later on.

Hunting around on the web turned up this; http://railsify.com/plugins/109-holly (which is written in Ruby and is complete garbage to me!), however the gist is to put a special comment block at the top of each javascript and css file that lists up & downstream rquirements which is then parsed by Holly and a correctly sequenced loading list of files is built.

I was thinking of perhaps extending the parsing logic to include some conditional checks.

Once this (complete) file list is build it could then be submitted to the cache builder. Hey presto only two files to load.. one javascript & one css.

What do you think?

Dave

[eluser]adamp1[/eluser]
Ummm I do like the sound of that, that way only the CSS and Javascript required is loaded. Only thing is I only see this being of any use say, when you have javascript libraries which are only loaded on some pages (say that they need the jQuery base library, this way jQuery would only be loaded when needed).

You say things get over ridden when loading admin styles after shared. Surely this will happen also with the Holly approach, I don't see this overriding thing as an issue. Surely that's a feature you want in CSS.

If you see how I have structured css files shared files are either very basic (I.E reset browser styles to default) or little css files used throughout (I.E the style for status messages).

So I do like the idea but can't really see much use for it (it would be a nice feature but I don't think it is as necessary as having conditional css style sheets working atm).

Finals are this next week so all I am looking at now is lots and lots of books

[eluser]ddrury[/eluser]
OK

I take your point, however I really should allow time for my brain to wake up before posting messages.

Of course any file where loading is conditional should have it's own load line in the generated html (cached or not) otherwise it wouldn't be conditional!!!

Dave

[eluser]adamp1[/eluser]
Yes, from this I think I have a brief idea of what I will do.

1) As you have mentioned allow conditional files (CSS only) to be specified in the config file.
2) Implement caching as currently, but only cache shared/public/admin files, conditional files will always be loaded separately.
3) Implement jsmin and csstidy plugin libraries to allow for better compression of cache.

I think those several features should allow the Asset system to be both simple and powerful.




Theme © iAndrew 2016 - Forum software by © MyBB