Le répertoire View dans Magento 2

Dans Magento 2, the view répertoire plays a crucial role in defining how your module's contenu is displayed on the frontend and back-office.

This répertoire contains all the necessary fichiers to manage the layout, templates, and static contenu. Understanding the structure and usage of the view dossier is essential for développeurs looking to customize the visual aspects of their Magento 2 modules.

Structure of the View Directory

app/code/{Vendor}/{Module}/view
│
├── adminhtml/
│   ├── layout/
│   ├── templates/
│   ├── web/
│
├── frontend/
│   ├── layout/
│   ├── templates/
│   ├── web/
  • adminhtml/: Contains fichiers lié à the Magento 2 panneau d'administration (back-office).
  • frontend/: Contains fichiers for the frontend or the store that clients interact with.
  • layout/: Stores XML fichier de layouts, which define how the page is constructed.
  • templates/: Stores PHTML fichier de templates, which contain the HTML and PHP code to display contenu.
  • web/: Stores static assets tel que CSS, JavaScript, and images.

Key Components in the View Directory

  1. Layout Files (layout/)
    Layout fichiers are written in XML and are responsible for determining the structure of pages. They define which blocks are rendered, their position on the page, and how they interact.
    
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column">
        <body>
            <referenceContainer name="content">
                <block class="Magento\Framework\View\Element\Template" name="custom.block" template="Vendor_Module::custom.phtml" />
            </referenceContainer>
        </body>
    </page>
    
    This XML fichier de layout specifies that a custom block devrait être added to the contenu container of a one-colonne layout.
  2. Templates (templates/)
    Template fichiers are written in PHTML and are used to render the HTML for blocks defined in the fichier de layouts. These fichiers can include logic and utilize Magento's block system to retrieve dynamic contenu.
    
    <h1>
        <?php echo $block->getTitle(); ?>
    </h1>
    <p>This is custom content rendered by a PHTML file in the view/templates folder.</p>
    
  3. Static Files (web/)
    The web répertoire holds static fichiers tel que CSS and JavaScript that peut être used to style and enhance the fonctionality of the module. These fichiers are automatically déployered to the pub/static répertoire.
    
    .custom-class {
        background-color: #f1f1f1; 
        padding: 10px;
    }
    
  4. Blocks
    Blocks are PHP classes that serve as the bridge between layout XML and template PHTML fichiers. Blocks can contain entreprise logic and pass data to the templates.
    
    namespace Vendor\Module\Block;
    
    class Custom extends \Magento\Framework\View\Element\Template
    {
        public function getTitle() {
            return __('This is a custom block title');
        }
    }
    

Extending and Overriding View Files

Magento 2's architecture allows développeurs to extend or override existing view fichiers from core modules. Par exemple, if you want to customize a template, you can place your custom fichier de template under the view/frontend/templates répertoire, and Magento will use it au lieu de the core fichier.

Conclusion

Understanding the view répertoire is vital for any Magento 2 développeur aiming to customize or extend the platform’s visual presentation. By working with fichier de layouts, templates, and static resources, you can create tailored utilisateur experiences for both the panneau d'administration and the frontend.