Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Managing Assets in Themes and Template
  • Dear,

    I am a newbee,

    some of assets like css and js are not needed for every pages, so i have used this code to filter it out  in theme base_template.php

    <?php if(\Uri::segment(1) == 'user'): ?>
    <Link to user.css>
    <?php endif; ?>

    it is working, but it is not good way, it is not suitable when i changing theme,

    How we can do that? any built in fuel feature available?

  • HarroHarro
    Accepted Answer
    Use asset groups, and add the required assets to the group in the views that require them. And make sure the Views are rendered before the page template.

    Alternatively, use Theme for your views, that takes care of the correct rendering sequence automatically.

    So in your page template add this in the header:

        echo \Asset::render('header');

    and this in the footer

        echo \Asset::render('footer');

    In your view, you can then do something like

                \Asset::css("user", array(), 'header');
                \Asset::js("onlyonthispage", array(), 'footer');

    And it will be added to the page header (the css file) and footer (the js file) when the page template is rendered.
  • Dear,

    I using theme feature,

    When i use Asset::js(); i getting connot found asset error, What should i do?
  • HarroHarro
    Accepted Answer
    In case of Theme, you need to use the Theme asset instance, to make sure the theme paths are used to load the asset:

    echo \Theme::instance()->asset->render('header');

    and

    \Theme::instance()->asset->css("user", array(), 'header');
    \Theme::instance()->asset->js("onlyonthispage", array(), 'footer');

    and so on.

    This uses the default theme instance, if you use a named instance, you have to pass that name to the instance() method offcourse.
  • I getting this error.

    Fuel\Core\FuelException [ Error ]:
    Could not find asset: /global/plugins/jquery-validation/js/jquery.validate.min.js

    Exactly, the file is in

    /public/themes/<themename>/assets/global/plugins/jquery-validation/js/jquery.validate.min.js
  • Dear,

    My arrangement of asset file is also have some truble.

    I have put all plugins in plugins folder as seen in last post,

    and each plugins folder has css and js files of thet plugins,
    Any way to make it easy,
  • Make sure the asset path in your theme config is configured correctly. It looks like it can't find the asset.

    And you can add alternative search paths to the Asset instance, but you have to do it manually, there is no support for it in Theme:

    Theme::instance()->asset->add_path('/public/themes/<themename>/assets/global/plugins/jquery-validation', js');

    and the same for img and css...
  • Dear,
    I have around 30 plugins in plugins folder, so i have to do around 30x3=90 times the same sentence, Any clues.

    In my case i put seperate css,js,img folder in each plugins, I think if i created js,css,img in plugins folder and added each css,js,img to specific folder i may have to write only 3 add_path.

    eg:
    Theme::instance()->asset->add_path('/public/themes/<themename>/assets/global/plugins', js');
    Theme::instance()->asset->add_path('/public/themes/<themename>/assets/global/plugins', css');
    Theme::instance()->asset->add_path('/public/themes/<themename>/assets/global/plugins', img');

    am i true?
  • Only 30 times. ;-)

    You only have to do it once. If you don't give the type, the path will be added for all known types:

    Theme::instance()->asset->add_path('/public/themes/<themename>/assets/global/plugins');
  • If i done 30 times, will this affect software performance?
  • Ultimately a little bit, because when you want to load an asset, it will do a file_exists() to check if it exists, and if not, check the next path. If found it will also get the mtime() of the file.

    I don't think it's really noticable, unless you have a very high number of assets, or need to sqeeze every bit of performance out of your server (in which case you might want to consider serving assets from a subdomain using varnish).
  • I have used File::read_dir() to create array of directory and used foreach loop to do all of my file to Theme::instance()->asset->add_path();

    Working like charm, Thanks Dear

Howdy, Stranger!

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

In this Discussion