Am I missing something here? This seems quite an easy thing to do:

class AppController extends Controller {
  var $helpers = "SomeRandom";
}

class SomeRandomHelper extends AppHelper {
  var $helpers = array('Html', 'Javascript');

  function beforeRender() {
    // Load controller-specific css and js if exists
    $this->_loadIfExistsJs($this->params['controller']);
    $this->_loadIfExistsCss($this->params['controller']);

    // Load action-specific css and js if exists
    $this->_loadIfExistsJs($this->params['action']);
    $this->_loadIfExistsCss($this->params['action']);

    // Load controller+action-specific css and js if exists
    $this->_loadIfExistsJs($this->params['controller'], $this-
>params['action']);
    $this->_loadIfExistsCss($this->params['controller'], $this-
>params['action']);
  }

  function _loadIfExistsJs() {
    $bits = func_get_args();
    $file = JS . implode(DS, $bits) . '.js';
    if (file_exists($file)) {
      $this->Javscript->link(implode('/', $bits) . '.js', false);
    }
  }

  function _loadIfExistsCss() {
    $bits = func_get_args();
    $file = CSS . implode(DS, $bits) . '.css';
    if (file_exists($file)) {
      $this->Html->css(implode('/', $bits) . '.css', null, null,
false);
    }
  }
}

Assume that the controller is 'PostsController' and the action is
'view', then the following files will be loaded (if they exist):

/app/webroot/js/posts.js
/app/webroot/js/view.js
/app/webroot/js/posts/view.js

/app/webroot/css/posts.css
/app/webroot/css/view.css
/app/webroot/css/posts/view.css

The only gotcha I can see is if you're in an action 'bacon' and you
call $this->render('eggs') then it will be bacon.js that will be
loaded, not eggs.js - due to the way Controller::render() works. If
you use $this->setAction(), then it won't be a problem. Overcoming
this would require overriding AppController::render() to store the
first parameter somewhere accessible by the helper.

Just remember to have <?php echo $scripts_for_layout; ?> in your
layout and all will be well.

On Apr 10, 10:17 am, simon <[EMAIL PROTECTED]> wrote:
> is there a method to do the same for the above but for css sheets?
>
> On Apr 9, 10:14 am, "R. Rajesh Jeba Anbiah"
>
> <[EMAIL PROTECTED]> wrote:
> > On Apr 8, 8:59 am, John R <[EMAIL PROTECTED]> wrote:
>
> > > Is there an easy way with Cake to get /js/register.js to be
> > > automatically included in register.ctp? (And conversely not included
> > > if it doesn't exist?)
>
> >   Not automagic, but you may add this in the view file:
> > $this->addScript('register');
>
> > --
> >   <?php echo 'Just another PHP saint'; ?>
> > Email: rrjanbiah-at-Y!com    Blog:http://rajeshanbiah.blogspot.com/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to