Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Asset management
  • What is the correct way to have a controller determine which assets the view will require?
    This controller example:
    class Controller_Example extends Controller
    {
    
        public function action_index()
        {
            $data['css'] = Asset::css(array('reset.css','960.css','main.css'));
            return Response::forge(View::forge('welcome/index'));
        }
    }
    
    does nothing with the $data array, and I can't find a solution in the relevant "Asset" docs.
    TIA
  • There is no "correct" way, it all boils down to what your preferrences are, the kind of application you're building, how big your development team is, or if developers and designers are different people. For me personally assets are part of the design, and I therefore have them in the views. And I use the Theme class to separate the views from the code. This way the look-and-feel is completely separated from the logic, and to change it no code should be touched.
  • Well, my preference is often to have the controller responsible for determining which assets will be required by the view, and the example I have quoted deserves some explanation.
  • Take a look at canton7's Casset for FuelPHP. I've been using it since the start and I really love it: http://github.com/canton7/fuelphp-casset
  • That looks useful, Peter - thank you very much.
  • They only difference between Casset and FuelPHP's Asset class is the fact that it support minification. Otherwise, it's usage is exactly the same, which means you implement both the same way, and they both have the same limitations. You can either do:
    class Controller_Example extends Controller
    {
        public function action_index()
        {
            // create the view
            $view = View::forge('welcome/index');
    
            // assign the assets to the view variable 'css'. Use 'false' to prevent the HTML to be escaped
            $view->set('css',  Asset::css(array('reset.css','960.css','main.css')), false);
    
            // return the view
            return $view;
        }
    }
    
    // in your view
    
    <?php echo $css; ?>
    

    Alternatively you have the view render it.
    // controller
    class Controller_Example extends Controller
    {
        public function action_index()
        {
            // add the assets to the asset group 'header'
            Asset::css(array('reset.css','960.css','main.css')), array(), 'header');
    
            // return the view
            return View::forge('welcome/index');
        }
    }
    
    // in your view
    
    <?php echo Asset::render('header'); ?>
    

    In both cases you don't have a problem with FuelPHP's lazy view rendering. You will only have an issue with that if you assign assets to groups in view partials that need to be rendered in a page template. In that case the partials need to be rendered first. The Theme class will do this for you automatically.
  • Ah, okay. Thanks, Harro.

Howdy, Stranger!

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

In this Discussion