Outils pour utilisateurs

Outils du site


drupal9:themes_creation_theme_depuis_zero

Création d'un thème depuis zéro

Création du dossier

Nom du thème créé : mytheme

  • Créer le dossier /themes/custom/mytheme
  • Y créer l'arborescente suivante :
themes
    custom
        mytheme
            css
                style.css
            js
                main.js
            templates
                page.html.twig
            mytheme.info.yml
            mytheme.libraries.yml

Configuration du Thème

/themes/custom/mytheme/mytheme.info.yml
name: My Great Theme
description: This is my first theme project
type: theme
base theme: false
core_version_requirement: ^8 || ^9

regions:
  header: Header
  content: Content
  footer: Footer
  page_top: Page top
  page_bottom: Page bottom

libraries:
  - mytheme/global-css

page_top et page_bottom sont deux régions cachées, on ne peut pas y placer de bloc, mais elles doivent exister. Comme on déclare au moins une région, il faut les déclarer toutes, y compris ces deux-ci.

Librairies

/themes/custom/mytheme/mytheme.libraries.yml
global-css:
  version: VERSION
  css:
    theme:
      css/elements.css: {}
      css/layout.css: {}
      css/print.css: { media: print }
  js:
    js/main.js: {}
  dependencies:
    - core/jquery

Structure des templates du core

x-- core/modules/system/templates/html.html.twig
|
|   <html>
|       <head>
|       </head>
|
|       <body>
|
|       x-- core/modules/toolbar/templates/toolbar.html.twig
|       |
|       x--
|
|       x-- core/modules/system/templates/off-canvas-page_wrapper.html.twig
|       |
|       |   x-- core/modules/system/templates/page.html.twig
|       |   |
|       |   x--
|       |
|       x--
|
|       </body>
|   </html>
|
x--

Réécrire un template

/themes/custom/mytheme/templates/page.html.twig
<!-- région "header" -->
<div class="header">
    {{page.header}}
</div>
<!-- région "content" -->
<div class="content">
    {{page.content}}
</div>
<!-- région "footer" -->
<div class="footer">
    {{page.footer}}
</div>
drupal9/themes_creation_theme_depuis_zero.txt · Dernière modification: 2022/08/08 05:52 par marclebrun