Jump to:

5542 Posts in 1739 Topics by 1225 members

Customising the CMS

SilverStripe Forums » Customising the CMS » The Solution -> combined_files

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 3927 Views
  • PGiessler
    Avatar
    Community Member
    47 Posts

    The Solution -> combined_files Link to this post

    Hello,
    I've a problem with the function Requirements::combine_files. The function doesn't generate the files, in the example the files should be called "cssmin.css" and "jsmin.js".

    Maybe I have a mistake in my source code. But I can't find it. I hope you can help me with this problem.

    $theme = SSViewer::current_theme();
          echo $path = 'themes/'.$theme.'/';
          
    Requirements::combine_files(
       $path.'css/cssmin.css',
       array(
          $path.'css/layout.css',
          $path.'css/typography.css',
          $path.'css/form.css',
          $path.'css/print.css',
          $path.'css/highslide.css',
       )
          );
          
    Requirements::combine_files(
       $path.'js/jsmin.js',
       array(
    'jsparty/prototype.js',
                'jsparty/behaviour.js',
                'jsparty/prototype_improvements.js',
                'sapphire/javascript/i18n.js',
                'sapphire/javascript/Validator.js',
          $path.'js/highslide.js',
          $path.'js/stars.js',
       )
          );      

  • bschmitt
    Avatar
    Community Member
    22 Posts

    Re: The Solution -> combined_files Link to this post

    I've quite the same problem. Has anyone a working example of the "combine_files" usage?

    At the docs is an example but I guess this is outdated: http://doc.silverstripe.org/doku.php?id=recipes:combining_files because it didn't worked for me.

    Many thanks and best,
    Bjoern

  • bschmitt
    Avatar
    Community Member
    22 Posts

    Re: The Solution -> combined_files Link to this post

    Hi

    I guess there's a bug within the Requirements::process_combined_files() method. I was not able getting this running without adjusting the code a little. The problem is that during processing of the combined files no file of the combined array will be added to the central javascript or css-holder array why no file is set for rendering on the page.

    Here is my working example:

    1. Add the following code to sapphire/core/ Requirements.php -> class Requirements_Backend -> function process_combined_files() [line ~790]

    function process_combined_files() {

    if((Director::isDev() && !SapphireTest::is_running_test()) || !Requirements::get_combined_files_enabled()) {
       return;
    }

    // Make a map of files that could be potentially combined
    $combinerCheck = array();
    foreach($this->combine_files as $combinedFile => $sourceItems) {
    foreach($sourceItems as $sourceItem) {
          if(isset($combinerCheck[$sourceItem]) && $combinerCheck[$sourceItem] != $combinedFile){
           user_error("Requirements::process_combined_files - file '$sourceItem' appears in two combined files:" .   " '{$combinerCheck[$sourceItem]}' and '$combinedFile'", E_USER_WARNING);
          }
          $combinerCheck[$sourceItem] = $combinedFile;

          // FIX start
          switch(end(explode(".",$sourceItem))) {
           case "js" : $this->javascript($sourceItem); break;
           case "css" : $this->css($sourceItem); break;
          }
          // FIX end
       
       }
    }
    ...

    2. Use this code within your Page.php

    public function init() {
    parent::init();

    // packs css and js files to a single file, only in live mode
    $theme = SSViewer::current_theme();
    $path = 'themes/'.$theme.'/';

    Requirements::combine_files(
        $path.'css/cssmin.css',
        array(
        $path.'css/layout.css',
    $path.'css/typography.css',
          $path.'css/form.css',
    )
    );

    Requirements::combine_files(
        $path.'js/jsmin.js',
    array(
        'jsparty/prototype.js',
          'jsparty/behaviour.js',
          'jsparty/prototype_improvements.js',
          'sapphire/javascript/i18n.js',
          'sapphire/javascript/Validator.js',
        )
    );
          
    Requirements::process_combined_files();      
       
    }

    Please also make sure that you are not in "dev" mode, cause this feature will only work in "live" mode.

    Director::set_environment_type("live");

    Best regards, Bjoern

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: The Solution -> combined_files Link to this post

    You're both trying to use the system incorrectly - if you look through the other places in the source where combine_files() is used, you'll note that you have to call Requirements::css/javascript(), as well as the combine_files() call.

    Requirements::css('foo');
    Requirements::css('bar');

    Requirements::combine_files('baz', array('foo', 'bar'));

  • PGiessler
    Avatar
    Community Member
    47 Posts

    Re: The Solution -> combined_files Link to this post

    Thank you for your help. I will try this solution in the next days.

  • PGiessler
    Avatar
    Community Member
    47 Posts

    Re: The Solution -> combined_files Link to this post

    Only one of these mentioned solution can solve my problem.

    If I apply the solution of ajshort, SilverStripe only implements the single files, because I call the function Requirements::css so the files will implement automatically in the header area of my side. If you look this in the source code, you will find this and come to my conclusion.

    Furthermore the process of combined_files() doesn't work, so SilverStripe isn't able to create the files. I checked this with the Development Environement (dev).

    Now I tried the solution of bschmitt, and it's works! SilverStripe implements the combined files, who I wrote in the array(). So I guess this is a bug in the function "combined_files. ".

    Many thanks to both of you.

    Pascal

  • PGiessler
    Avatar
    Community Member
    47 Posts

    Re: The Solution -> combined_files Link to this post

    Today I wrote a solution, which you don't have to adjusting the sapphire source code. Furthermore only the combined files will implement into the theme, so there are no double files.
    Many thanks to ajshort! You had the right approach to the problem!

    And here is the source code to use combined_files: (Director::set_environment_type("live")

    public function init() {
          parent::init();

           // set the path of the files
             $theme = SSViewer::current_theme();
             $path = 'themes/'.$theme.'/';

             //Register the file extension
             Requirements::css($path.'css/layout.css');
             Requirements::css($path.'css/typography.css');
             Requirements::css($path.'css/form.css');
             Requirements::javascript('sapphire/javascript/i18n.js');
             Requirements::javascript('sapphire/javascript/Validator.js');
             Requirements::javascript('jsparty/behaviour.js');
             Requirements::javascript('jsparty/prototype.js');
             Requirements::javascript('jsparty/prototype_improvements.js');

             /*
             Start the process of combine_files. The combined files will
             included automatically in our theme. Because of this you don't
             have to write <% require ... %>
             */

             Requirements::combine_files (
           $path.'css/cssmin.css',
           array(
           $path.'css/layout.css',
             $path.'css/typography.css',
             $path.'css/form.css',
             )
             );

             Requirements::combine_files (

             $path.'css/jsmin.js',
           array(
           'sapphire/javascript/i18n.js',
             'sapphire/javascript/Validator.js',
             'jsparty/prototype.js',
             'jsparty/behaviour.js',
             'jsparty/prototype_improvements.js'
                   )
             );

             /*
             we doesn't need these files anymore, because they are included in
             cssmin.css and jsmin.js'. So we start the process clear()
             */
             Requirements::clear('sapphire/javascript/i18n.js');
             Requirements::clear('sapphire/javascript/Validator.js');
             Requirements::clear('jsparty/behaviour.js');
             Requirements::clear('jsparty/prototype.js');
             Requirements::clear('jsparty/prototype_improvements.js');
             Requirements::clear($path.'css/layout.css');
             Requirements::clear($path.'css/typography.css');

       }

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: The Solution -> combined_files Link to this post

    instead of using $path = 'themes/'.$theme.'/'; then $Path you should be able to do

    Requirements::themedCSS('filename'); // themes/yourcurrenttheme/css/filename.css

    3927 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.