Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Symfony: The Fast Track
  4. Italian
  5. Impostare lo stile dell'interfaccia utente con Webpack

Impostare lo stile dell'interfaccia utente con Webpack

Non abbiamo speso tempo nella progettazione dell'interfaccia utente. Per impostare uno stile professionale, utilizzeremo uno stack moderno basato su Webpack, e per aggiungere un tocco di Symfony e facilitare la sua integrazione con l'applicazione, installiamo Webpack Encore:

1
2
$ symfony composer rem asset-mapper
$ symfony composer req encore

Un ambiente Webpack completo è stato creato per noi: package.json e webpack.config.js sono stati generati e contengono una buona configurazione predefinita. Aprendo webpack.config.js, potremo notare l'utilizzo dell'astrazione Encore usata per configurare Webpack.

Il file package.json definisce alcuni comandi che useremo sempre.

La cartella assetscontiene i principali punti di ingresso per le risorse del progetto: styles/app.css e app.js.

Utilizzo di Sass

Invece di usare i normali CSS, passiamo a Sass:

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

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

Installare sass-loader:

1
$ npm install node-sass sass-loader@13 --save-dev

E abilitare il Sass loader in webpack:

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()

Come facevo a sapere quali pacchetti installare? Se si tenta un build senza pacchetti, Encore mostra un messaggio di errore e suggerisce il comando npm install per installare le dipendenze (necessarie per caricare i file .scss).

Sfruttare Bootstrap

Per iniziare con buone impostazioni predefinite e costruire un sito responsive, un framework CSS come Bootstrap può aiutarci molto. Installiamolo come pacchetto:

1
$ npm install bootstrap @popperjs/core bs-custom-file-input --save-dev

Richiediamo Bootstrap nel file CSS (abbiamo anche ripulito il file):

1
2
3
4
5
6
7
--- a/assets/styles/app.scss
+++ b/assets/styles/app.scss
@@ -1,3 +1 @@
-body {
-    background-color: lightgray;
-}
+@import '~bootstrap/scss/bootstrap';

Facciamo lo stesso per il file JS:

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

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

Il sistema dei form di Symfony supporta Bootstrap nativamente con un tema speciale, abilitiamolo:

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

Impostare lo stile dell'HTML

Ora siamo pronti per impostare lo stile dell'applicazione. Scarichiamo ed estraiamo l'archivio nella cartella principale del progetto:

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

Dando un'occhiata ai template, potreste imparare un paio di trucchi su Twig.

Build degli asset

Un cambiamento importante quando si usa Webpack è che i file CSS e JS non sono utilizzabili direttamente dall'applicazione. Devono essere prima "compilati".

Durante lo sviluppo, gli asset possono essere compilati tramite il comando encore dev:

1
$ symfony run npm run dev

Invece di eseguire il comando ogni volta che c'è una modifica, metterlo in background e lasciare che intercetti le modifiche dei file JS e CSS:

1
$ symfony run -d npm run watch

Prendetevi il tempo per scoprire i cambiamenti visivi. Date un'occhiata al nuovo design in un browser.

/
/conference/amsterdam-2019

Il form di login generato ha ora uno stile, perché MakerBundle usa di default le classi CSS di Bootstrap:

/login

In produzione, Platform.sh rileva automaticamente che si sta usando Encore e compila gli asset per noi durante la fase di build.

Andare oltre

  • Documentazione di Webpack;
  • Documentazione di Webpack Encore;
  • Guida a Webpack Encore su SymfonyCasts.
Previous page Caching per le prestazioni
Next page Ridimensionamento delle immagini
This work, including the code samples, is licensed under a Creative Commons BY-NC-SA 4.0 license.
TOC
    Version

    Symfony 6.4 is backed by

    Show your Symfony expertise

    Show your Symfony expertise

    Put the code quality back at the heart of your project

    Put the code quality back at the heart of your project

    Version:
    Locale:
    ebook

    This book is backed by:

    see all backers

    Symfony footer

    Avatar of Piergiuseppe Longo, a Symfony contributor

    Thanks Piergiuseppe Longo for being a Symfony contributor

    1 commit • 3 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony