How to include css file for a specific view?

edited May 2014 in Share
How can I include a css file only for a specific view in FuelCMS? Something along the lines of:
if (!empty($is_blog)):
echo css('blog').css($css);
endif;
If this is possible, where should I set the $is_blog variable so that it's recognized in the header.php block?

Comments

  • edited May 2014
    You can set a variable called $css and then in your header.php file do something like the following:
    <?php if (!empty($css)) : echo css($css); endif; ?>
    In your specific view file you can use fuel_set_var at the top of your view file:
    <?php fuel_set_var('css', 'mycss')?>
  • edited May 2014
    Could you please elaborate on the meaning of css('blog').css($css);
    I can't seem to find the relevant document in the user guide section.
    And also please tell me what is 'mycss' in your code. Is it the name of a css file? What about its path?
    Thank you
  • edited 11:06AM
    Basically you have your header file look for a variable called $css (first piece of code above). This should be located under the html -> head tag. That variable's value will be the name of the CSS file you want to include and should be located in the assets/css/ folder (in the example above it would have been assets/css/mycss.css ... the extension is optional for the css function).

    Then in you view file, you add the fuel_set_var code (second piece of code above) at the top. This will set the variable with the name of "css" to be "mycss". Does that help.
  • edited 11:06AM
    Thank you very much for your help. It's clear now. Just two more question:

    1- Is the "css()" function documented anywhere in the user guide? If yes, where? and if no, why?

    2- I understand the meaning of css($css). But what's the meaning of "css('blog').css($css)"? This piece of code is found in the default header file installed by fuelcms.

    Kind regards.
  • edited May 2014
    Hi again,

    There is an 'echo' missing in your code. It should be this:
    if (!empty($css)) : echo css($css); endif;
  • edited May 2014
    Ah... yes. Thanks. I've updated it above too.
  • edited 11:06AM
    Regarding your question about the "css" helper function, there is some documentation that can be found here:
    http://docs.getfuelcms.com/helpers/asset_helper#func_css
    http://docs.getfuelcms.com/libraries/asset

    Regarding:
    echo css('main').css($css);
    The css function simply outputs a link tag and so this is just concatenating them and outputing the string as opposed to 2 separate echo calls.
Sign In or Register to comment.