SymfonyWorld Online 2020
100% online
30+ talks + workshops
Live + Replay watch talks later

Step 22: Styling the User Interface with Webpack

5.0 version
Maintained

Styling the User Interface with Webpack

We have spent no time on the design of the user interface. To style like a pro, we will use a modern stack, based on Webpack. And to add a Symfony touch and ease its integration with the application, let’s install Webpack Encore:

1
$ symfony composer req encore

A full Webpack environment has been created for you: package.json and webpack.config.js have been generated and contain good default configuration. Open webpack.config.js, it uses the Encore abstraction to configure Webpack.

The package.json file defines some nice commands that we will use all the time.

The assets directory contains the main entry points for the project assets: css/app.css and js/app.js.

Using Sass

Instead of using plain CSS, let’s switch to Sass:

1
$ mv assets/css/app.css assets/css/app.scss
patch_file
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
--- a/assets/js/app.js
+++ b/assets/js/app.js
@@ -6,7 +6,7 @@
  */

 // any CSS you import will output into a single css file (app.css in this case)
-import '../css/app.css';
+import '../css/app.scss';

 // Need jQuery? Install it with "yarn add jquery", then uncomment to import it.
 // import $ from 'jquery';

Install the Sass loader:

1
$ yarn add node-sass "[email protected]^7.0.1" --dev

And enable the Sass loader in webpack:

patch_file
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -54,7 +54,7 @@ Encore
     })

     // enables Sass/SCSS support
-    //.enableSassLoader()
+    .enableSassLoader()

     // uncomment if you use TypeScript
     //.enableTypeScriptLoader()

How did I know which packages to install? If we had tried to build our assets without them, Encore would have given us a nice error message suggesting the yarn add command needed to install dependencies to load .scss files.

Leveraging Bootstrap

To start with good defaults and build a responsive website, a CSS framework like Bootstrap can go a long way. Install it as a package:

1
$ yarn add bootstrap jquery popper.js bs-custom-file-input --dev

Require Bootstrap in the CSS file (we have also cleaned up the file):

patch_file
1
2
3
4
5
6
7
--- a/assets/css/app.scss
+++ b/assets/css/app.scss
@@ -1,3 +1 @@
-body {
-    background-color: lightgray;
-}
[email protected] '~bootstrap/scss/bootstrap';

Do the same for the JS file:

patch_file
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
--- a/assets/js/app.js
+++ b/assets/js/app.js
@@ -7,8 +7,7 @@

 // any CSS you import will output into a single css file (app.css in this case)
 import '../css/app.scss';
+import 'bootstrap';
+import bsCustomFileInput from 'bs-custom-file-input';

-// Need jQuery? Install it with "yarn add jquery", then uncomment to import it.
-// import $ from 'jquery';
-
-console.log('Hello Webpack Encore! Edit me in assets/js/app.js');
+bsCustomFileInput.init();

The Symfony form system supports Bootstrap natively with a special theme, enable it:

config/packages/twig.yaml
1
2
twig:
    form_themes: ['bootstrap_4_layout.html.twig']

Styling the HTML

We are now ready to style the application. Download and expand the archive at the root of the project:

1
2
3
$ php -r "copy('https://symfony.com/uploads/assets/guestbook.zip', 'guestbook.zip');"
$ unzip -o guestbook.zip
$ rm guestbook.zip

Have a look at the templates, you might learn a trick or two about Twig.

Building Assets

One major change when using Webpack is that CSS and JS files are not usable directly by the application. They need to be “compiled” first.

In development, compiling the assets can be done via the encore dev command:

1
$ symfony run yarn encore dev

Instead of executing the command each time there is a change, send it to the background and let it watch JS and CSS changes:

1
$ symfony run -d yarn encore dev --watch

Take the time to discover the visual changes. Have a look at the new design in a browser.

The generated login form is now styled as well as the Maker bundle uses Bootstrap CSS classes by default:

For production, SymfonyCloud automatically detects that you are using Encore and compiles the assets for you during the build phase.


This work, including the code samples, is licensed under a Creative Commons BY-NC-SA 4.0 license.