Your theme have to print assets correctly in the smarty template, and it’s explained in the template section.
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.
We introduced new methods to register assets, and especially new cool options.
For instance, you can register your asset specifically in the head or bottom of your HTML code; you can load it
with attributes like
defer; and you can even inline it easily.
One favorite option is the priority one, which makes it very easy to ensure everything is loaded in the order you need.
Backward compatibility is kept for the
addJqueryPlugin() methods. Incidentally, now is the best time to update your libraries and use the new method.
Here is a list of options, and what they do.
PrestaShop’s FrontController class provides 2 new methods to easily register new assets:
In order to have the most extensible signatures, these 2 methods take 3 arguments. The first one is the unique ID of the asset, the second one is the relative path, and the third one is an array of all other optional parameters, as described below.
This unique identifier needed for each asset. This is useful to either override or unregister something already loaded by the Core or a native module.
This is the path of your asset. In order to make your assets fully overridable and compatible with the parent/child feature, you need to provide the path from the theme’s root directory, or PrestaShop’s root directory if it’s a module.
- ‘assets/css/example.css’ for something in your theme.
- ‘modules/modulename/css/example.cs’ for something in your module.
Extra parameters for stylesheet
|priority||0-999 (default: 50)||0 is the highest priority|
|inline||true|false (default: false)||If true, your style will be printed inside the
|priority||0-999 (default: 50)||0 is the highest priority|
|inline||true|false (default: false)||If true, your style will be printed inside
Registered by the Core¶
Every page of every theme loads the following files:
- rtl.css (if a right-to-left language is detected)
|theme.css||theme-main||0||Most (all?) of your theme’s styles. Should be minified.|
|rtl.css||theme-rtl||900||Loaded only for Right-To-Left language|
|custom.css||theme-custom||1000||Empty file loaded at the very end to allow user to override some simple style.|
|core.js||corejs||0||Provided by PrestaShop. Contains Jquery2, dispatches PrestaShop events and holds PrestaShop logic.|
|custom.js||theme-custom||1000||Empty file loaded at the very end, to allow user to override behavior or add simple script.|
Registering in themes¶
By now you probably understood that this
theme.yml file became the heart of PrestaShop themes.
To register assets, create a new
assets key at the top level of your
theme.yml, and register
your files according to your needs. Page identifiers are based on the
php_self property of
each controller (example)
For example, if you want to add an external library on each page and a custom library on the Product page:
assets: css: product: - id: product-extra-style path: assets/css/product.css media: all priority: 100 js: all: - id: this-cool-lib path: assets/js/external-lib.js priority: 30 position: bottom product: - id: product-custom-lib path: assets/js/product.js priority: 200 attribute: async
Registering in modules¶
When developing a PrestaShop module, you may want to add specific styles for your templates.
The best way is to use the
by the parent
inside the module: put it in the theme’s files instead (
With a front controller module¶
If you develop a front controller, simply extend the
setMedia() method. For instance:
Without a front controller module¶
If you only have your module’s class, register your code on the actionFrontControllerSetMedia hook, and add your asset on the go inside the hook:
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
Let’s say you want to be fully compatible with a popular navigation module. You could create a template
override of course, but you could also remove the style that comes with it and
bundle your specific style in your
theme.css (since it’s loaded on every page).
To unregister an assets, you need to know its ID.
As of today, the only way to unregister an asset without any module is to place an empty file where the module override would be.
views/js/file.js, you simply need
to create an empty file in
unregisterStylesheet methods take only one argument:
the unique ID of the resource you want to remove.
Webpack is a module bundler. Webpack takes modules with dependencies and generates static assets representing those modules.
The main interest of using Webpack is that it will compile all your styles - which we advise you to write using Sass - into a single CSS file. This way, your theme will make only one HTTP request for this single file, and since your browser will cache it for later re-use, it will even donwload this file only once.
Webpack is not at all required by PrestaShop, you are free to use your favorite tool! The documentation explains Webpack since it’s the tool we chose for the Classic theme, and StarterTheme ships with a ready-to-use configuration file.
If you want to compile your assets using Webpack (and we advise you to), follow these steps:
- Download and install Node.js, which contains the npm tool.
- In your command line tool, open the _dev folder.
- Install npm: npm install.
- You then have a choice:
- To build your assets once, type npm run build.
- To rebuild your assets every time you change a file in the _dev folder, type npm run watch.
The Webpack configuration file for StarterTheme is thus:
- All CSS rules go to the assets/css/theme.css file.
It provides proper configuration for compile your Sass, Less, Stylus or CSS files into a single CSS file.
If you want to use Stylus or Less, simply edit the command line under the “scripts” section.