Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Autoloading Javascript and CSS?
  • Is it difficult to setup all controllers in an application to:
    1.) Check for the existence of a javascript file with the same name as the controller, and automatically include it in the view output?
    2.) Do the same with CSS files? Load them IF they exist?
    3.) Perhaps do the same if a Model exists... I'd like the framework to proceed without complaint if a file does not exist. I know this is possible, but what would be involved? How simple would this be in Fuel?
    Would you simply copy the controller file from the framework core to a library folder in your application and extend it to include this functionality? I'm brand new to Fuel, but am considering using it for a new CMS application. Thank you.
  • Use a base Controller, and in that controller (either in the constructor or the before() method, use something like
    // add our controller js if exists
    $js = \Asset::find_file( get_class( $this ), 'js' ) ) AND \Asset::js( get_class( $this ) );
    
  • Excellent. Thank you. If I did this in the base controllers constructor, Would I then have to make sure and $view->set('auto_js',$this->auto_js);
    for the particular view that I load in my action? Or could I just do that in the before(), or in the after() ?
  • Depends on where you create your view object. You could also use set_global(), so the variable is available in all views loaded.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion