In this post I am going to explain how to add remote javascript & stylesheet in PrestaShop 1.7.
In PrestaShop 1.7+, it’s easy to register custom assets on each pages. The major improvement is that you can easily manage them from your theme, without any modules.
Lets see how to registering & unregistering javascript as well as stylesheet easily.
Registering in modules
1) In a front controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $this->registerStylesheet( 'module-modulename-style', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css', [ 'media' => 'all', 'priority' => 200, ] ); $this->registerJavascript( 'module-modulename-simple-lib', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', [ 'priority' => 200, 'attribute' => 'async', ] ); |
2) In a module class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $this->context->controller->registerStylesheet( 'module-modulename-style', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css', [ 'media' => 'all', 'priority' => 200, ] ); $this->context->controller->registerJavascript( 'module-modulename-simple-lib', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', [ 'priority' => 200, 'attribute' => 'async', ] ); |
Unregistering
You can unregister assets! That’s the whole point of an id. For example if you want to improve your theme/module’s compatibility with a module, you can unregister its assets and handle them yourself.
Both unregisterJavascript and unregisterStylesheet methods take only one argument: the unique ID of the resource you want to remove..
1) In a front controller
1 2 | $this->unregisterJavascript('js-identifier'); $this->unregisterStylesheet('css-identifier'); |
2) In a module class
1 2 | $this->context->controller->unregisterJavascript('js-identifier'); $this->context->controller->unregisterStylesheet('css-identifier'); |
Thats it!
Leave a Reply