Erhan is typing…

WebDevelopment , PHP, Javascript, CakePHP, ExtJS

CakePHP Helper for rakaz Combine

| Comments

Combine is still my favorite javascript/css combine and compress script.

I posted a solution how to use it in a CakePHP application and here is a little helper to make it more useful:

app/views/helpers/combine.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
class CombineHelper extends AppHelper
{
  public $helpers = array('Html', 'Javascript');
  
  private $_pattern = '../combine.php?type=:type&files=:files';
  
  public function js($files)
  {
      echo $this->Javascript->link($this->_format($files));
  }
  
  public function css($files)
  {
      echo $this->Html->css($this->_format($files, 'css'));
  }
  
  private function _format($files = array(), $type = 'javascript')
  {
      return String::insert($this->_pattern, array('type' => $type, 'files' => implode(',', $files)));
  }
}

Add it to controller’s helpers property:

1
2
3
4
<?php
class MyController extends AppController
{
  public $helpers = array('Combine');

And call it by passing an array of file names in your view:

1
2
3
4
5
6
7
8
9
10
11
$combine->js(array(
    'javascript1.js',
    'javascript2.js',
    'javascript3.js'
));

$combine->css(array(
    'stylesheet1.css',
    'stylesheet2.css',
    'stylesheet3.css'
));

Don’t forget to add file extensions!

If you want to add only one file, you don’t have to use combine helper. Directives added to .htaccess file let combine script to compress the file(See related post). Just use $javascript->link('filename').

Comments