add globally style and script in concrete5

220 views Asked by At

I am using concrete5 8.0.1 version. I have created a Add-on. Now i want to add a script and a style tag globally with the help of my add-on means if i enable setting from my add-on then one script and one style tag will added on overall site. Currently i am using below code but it's working only for my controller. If i click on other menu script and style tag will not load.

$this->addHeaderItem('<style type="text/css"> .mytyle { display:none;}</style>');
$this->addHeaderItem('<script> alert("Here")</script>');
1

There are 1 answers

0
1stthomas On

In the package controller you can add following lines:

public function on_start()
{
    $al = \Concrete\Core\Asset\AssetList::getInstance();
    $al->register(
        'javascript', 'yourhandle', 'path/to/file/your-javascript.min.js'
    );
    $al->register(
        'css', 'yourhandle', 'path/to/file/yourcss.min.css'
    );
    $view = \View::getInstance();
    $view->requireAsset('css', 'yourhandle');
    $view->requireAsset('javascript', 'yourhandle');
}

After you installed the package with this controller, these assets will be loaded on every page load.

Src: concrete5 - 1 and concrete5 - 2