Search code examples
cssoctobercmspartialsoctobercms-backend

On Octobercms inject CSS from Partial


I try to inject Javascript and Css from partial using this:

function onStart()
{
    $this->addCss('assets/css/style1.css');
    $this->addCss('assets/css/style2.css');
    $this->addJs('assets/js/javascript1.js');
    $this->addJs('assets/js/javascript2.js');

}

On my layout i'm using {% styes %} and {% scripts %}, however only javascripts are succesfully injected. Css are not injected.

I also used {% put scripts %} and {% put styles %} but only scripts are injected again.

If i use the above code directly on my layout.htm css are injected, but i need this function on my partials. Is this possible?


Solution

  • It seems {% styles %}is respecting hierarchy.

    means if you use {% styles %} before your partial it will not inject css for that partial.

    so your partial which are coming after {% styles %} , it do not have information of it so it will not inject styles from it.

    for example. you are including css/js in code section onStart in your partial then,

    This will work

    <!-- it will work -->
    {% partial 'site/meta' %} <- you are injecting styles in code section
    {% styles %} <- its after partial
    

    This will not work

    <!-- it will not work -->
    {% styles %} <- its before partial
    {% partial 'site/meta' %} <- you are injecting styles in code section
    

    So, we can just make sure we include/inject all styles then use {% styles %} so it have all information about css included/injecting. then it can render all the style tags with css.

    if any doubt please comment.