Client script - including the whole script directory

How about creating a method in the CClientScript, that registers the whole directory:

public function registerScriptDirectory($dir, $makeAsset = true)


{


//loop through files, applu CHtmp::asset to them if $makeAsset == true


}

This should be very convenient, I think…

Alternative approach to the batch inclusion is passing an array of files and base bath (where they are) there.

Not sure if this is a good idea, but this is very costly because it involves traversing the directory. If you really want it, you may extend CClientScript and replace the existing "clientScript" component.

Thanks! Solved it and described solution in details here: http://programmersno…s-files-in-yii/

Very nice writing!

Yeah can be nice, and nicely written. You might want to cache the result for a while instead of pushing it all to registerScriptFile() in the loop.

Do you mean to form a new file from all that? Or how can I do "registerAll"?

Well, that's a nice option as well, you could take a look at jsmin and cssmin for that. But I meant something like this:



public function registerScriptDirectory($path, $makeAssets = true, $position = self::POS_HEAD)


{


	$id = md5(serialize($path.$makeAssets.$position));


	$data = Yii::app()->cache->get($id);





	if($data === false)


	{


		$di = new DirectoryIterator($path);        


		foreach ($di as $file)        


		{            


			if (!$file->isDot() && !$file->isDir())            


			{


				$data[] = $makeAssets ? CHtml::asset($path.$file->getFileName()) : $path.$file->getFileName();


			}        


		}   


		Yii::app()->cache->set($id, $data, Yii::app()->params->cacheTime);


	}


	


	foreach ($data as $file)


	{


		$this->registerScriptFile($file, $position);


	}


}


Thanks! Tested this, it works great :) I’ve updated the blog post.

Cool! Saw a new feature in CClientScript: scriptMap. Not sure yet how to use it, but the description sounds interesting…

yes, I saw that, but I also don’t know see how I can use it :)

scriptMap works the other way around, but a couple of the 1.0.3 functios in CClientScript will give some nice options. Will rework my JSmin and CSSmin lib to work a bit closer with these additions and share it.

scriptMap is described in detail here:

http://www.yiiframew…ics.performance

@Konstantin: You might want to take a look at http://www.yiiframew…opic,988.0.html. Perhaps there is some for you to combine it with your solution.

Thanks! However, I can’t do it now - urgent project, but I’ve planned this for later. If you do it before me (I’ll have time next weekends) - let me know, I’m interested in using such thing :)

Konstantin: I made an extension: ExtendedClientScript. You can easily extend that one with yours, and (hopefully) take advantage of the compression options.

Thanks for letting me know, I'll test it these weekends.