jump to navigation

Register javascript & css files from helpers March 23, 2006

Posted by rossoft in CakePHP.
trackback

If you have a helper that needs some javascript / css files that you want to include, you can include them in body or head.
It’s easy to include them in body, but if you want in head then you have to break a little your layout or include the file for all views.

I made a little helper for register the files you want from another helper, then print all the files registered in <head>.

In your own helper:
var $helpers=array(‘xxx’,’myhtml’);

function myfunc(xxx)
{
    $this->myhtml->register(‘tooltip’ , ‘js’);
    $this->myhtml->register(‘somecss’ , ‘css’);
}

In your layout, in head tag:
<? if (isset($myhtml)) echo $myhtml->print_registered(); ?>

Be sure to include myhtml helper in your appcontroller

<?
    class MyhtmlHelper extends Helper
    {
        var $helpers=array(‘html’,’javascript’);
       
           var $_library; //static array of files to be included            
          
           function __construct()
           {
               static $library;  //for php4 compat
               $this->_library=& $library;              
               $this->_library=array();             
           }
          
           /**
            * Adds a javascript file to array
            * @param string $file File to be included
            * @param type $type css | js
            */
           function register_file($file,$type)
           {
               if (! in_array($type,array(‘css’,’js’)))
               {
                die(“Myhtml_helper: Incorrect type: $type”);           
               }
               if (! in_array(array($file,$type),$this->_library))
               {
                   $this->_library[]=array($file,$type);
               }
                  
           }

        /**
         * Creates all the links to the files registered
         * @return string
         */          
           function print_registered()
           {
              foreach ($this->_library as $l)
              {
                  $file=$l[0];
                  $type=$l[1];
                  switch ($type)
                  {
                      case ‘css’:
                          echo $this->html->css($file);   
                          break;
                      case ‘js’:
                          echo $this->javascript->link($file);
                          break;
                  }
              }            
           }                                           
    }
?>

Advertisements

Comments»

1. Naonak - March 24, 2006

Very good !
I have a similar problem but for controller. I want to get css and js files from requestAction in controller to add it in the header. I find no solution.

2. Naonak - March 24, 2006

small mistake : register instead of register_file

In your own helper:
var $helpers=array(’xxx’,’myhtml’);

function myfunc(xxx)
{
$this->myhtml->register_file(’tooltip’ , ‘js’);
$this->myhtml->register_file(’somecss’ , ‘css’);
}

3. RosSoft - March 24, 2006

First, there is a mistake

function __construct()
{
static $library=array(); //for php4 compat
$this->_library=& $library;
}

Second,
You can register from component. You need access to the helper from controller. With little modifications this code will help you:
https://rossoft.wordpress.com/2006/02/11/cool-flash-effect-3/

Then in your action call the register_file function of the helper.

4. RosSoft » Maskedit Input helper - March 27, 2006

[…] 4. Copy this helper  to app/webroot/helpers/myhtml.php […]

5. RosSoft » Maskedit Input helper - March 27, 2006

[…] 4. Copy this helper and rename to HeadHelper to file: app/webroot/helpers/head.php […]

6. Richard@Home » Blog Archive » links for 2006-03-28 - March 28, 2006

[…] RosSoft » Register javascript & css files from helpers An extended helper that lets you specify additional css and js files. (tags: cakephp) […]

7. RosSoft » Register head tags from helpers (2) - March 28, 2006
8. Ankit Verma - March 20, 2009

registerin jscript

9. holarhr - February 24, 2011

In adept education, erudition by teaching designates currently the method past Jean-Pol Martin that allows pupils and students to prepare and to teach


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: