Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

The Solution -> combined_files


Go to End


10 Posts   8124 Views

Avatar
PGiessler

Community Member, 47 Posts

6 May 2009 at 1:14am

Edited: 11/05/2009 6:10am

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',
)
);

Avatar
bschmitt

Community Member, 22 Posts

7 May 2009 at 9:20pm

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

Avatar
bschmitt

Community Member, 22 Posts

9 May 2009 at 3:26am

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

Avatar
ajshort

Community Member, 244 Posts

9 May 2009 at 11:11am

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'));

Avatar
PGiessler

Community Member, 47 Posts

9 May 2009 at 10:51pm

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

Avatar
PGiessler

Community Member, 47 Posts

11 May 2009 at 1:49am

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

Avatar
PGiessler

Community Member, 47 Posts

11 May 2009 at 6:09am

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');

}

Avatar
Willr

Forum Moderator, 5523 Posts

11 May 2009 at 12:06pm

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

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

Go to Top