Load CSS and Javascript No Matter What
Permalink
I am working on a tool for Concrete that uses a Jquery plugin (javascript) and a CSS file.
I need these to be loaded no matter what - sitewide. It will be a part of the front-end toolbar. So far, I got the package to install a button to the toolbar. My problem is that the CSS and javascript don't show up in the footer and header.
Here is what I have my package controller:
The CSS and javascript live in /mypackage/css and mypackage/js.
How do I get them to show up?
I need these to be loaded no matter what - sitewide. It will be a part of the front-end toolbar. So far, I got the package to install a button to the toolbar. My problem is that the CSS and javascript don't show up in the footer and header.
Here is what I have my package controller:
public function on_start() { $al = AssetList::getInstance(); $al->register( 'css', 'ez_preview/style', 'css/previewer.css', array ( 'version' => '1.0', 'position' => Asset::ASSET_POSITION_HEADER, 'minify' => true, 'combine' => true), $this ); $al->register( 'javascript', 'ez_preview/script', 'js/previewer.js', array ( 'version' => '1.0', 'position' => Asset::ASSET_POSITION_FOOTER, 'minify' => true, 'combine' => true),
Viewing 15 lines of 21 lines. View entire code block.
The CSS and javascript live in /mypackage/css and mypackage/js.
How do I get them to show up?
This doesn't seem to work. I'm getting an error:
Call to a member function javascript() on a non-object
That's because $html hasn't been defined anywhere (it was just a rough example).
You could try something like:
Then just make sure you have a use Core; statement at the top of your package controller.
You could try something like:
$view->addFooterItem(Core::make('helper/html')->javascript('pathwhatever.js'));
Then just make sure you have a use Core; statement at the top of your package controller.
Ok, that helped a bit - the CSS and JS now show up in header and footer. But, I'm still having a problem. It is adding "concrete/css/" and "concrete/js/" in front of what's in my updated code below:
So, the JS has a URL like this now: "concrete/js/js/previewer.js".
It really should look like: "package/js/previewer.js", or have a full URL to it.
\Events::addListener( 'on_before_render', function ($e) { $view = $e->getArgument('view'); $view->addHeaderItem(Core::make('helper/html')->css('css/previewer.css')); $view->addFooterItem(Core::make('helper/html')->javascript('js/previewer.js')); } );
So, the JS has a URL like this now: "concrete/js/js/previewer.js".
It really should look like: "package/js/previewer.js", or have a full URL to it.
Try passing in your package handle:
$view->addHeaderItem(Core::make('helper/html')->css('css/previewer.css', 'packagehandle')); $view->addFooterItem(Core::make('helper/html')->javascript('js/previewer.js','packagehandle'));
So my suggestion would be to investigate using events, hooking into something like the on_before_render function.
Check this out for an example of an event in a package:http://documentation.concrete5.org/developers/packages/modifying-st...
I'd suggest doing setting it up as described but using the on_before_render event.
With the on_before_render event, the function gets passed an event object, and that object has within a view object, which I believe you can call 'addFooterItem' and 'addHeaderItem' on.
Here's a rough guess at what might work as a package function:
I think the main thing to watch out for is that this is going to apply to EVERY page in the site, so dashboard pages as well. So you'd have to put some code in to check that it's just a normal page that you're doing this one.
I've done this in 5.6 with something like this to check in the on_start():
But I've not tried this in 5.7. Hopefully that helps a bit!