Layouts

Contents

In this chapter you will add a two-column layout, which lets editors place components side by side.

Layouts are essentially parts with an extra trick up their sleeve: regions. This means that layouts may host other components, and build rich page structures.

Task: Add a layout to Enonic

Adding layouts is more or less identical to adding pages and parts.

  1. Check the layout descriptor - Verify that the Enonic app has the 2-column layout descriptor at:

    src/main/resources/cms/layouts/2-column/2-column.yaml
    kind: "Layout"
    title: "2 columns"
    description: "Provides left and right regions"
    form: [ ]
    regions:
      - "left"
      - "right"
    Like a page, layouts may declare named regions.
  2. Redeploy the Enonic app - This is only needed if you had to change it, and happens automatically if you started enonic dev.

Task: Add layout rendering to the front-end

Move over to the Next.js app, and add the component that renders the layout.

  1. Add the front-end layout component:

    src/components/layouts/TwoColumnLayout.tsx
    import type {LayoutProps} from '@enonic/nextjs-adapter';
    import React from 'react'
    import {RegionView} from '@enonic/nextjs-adapter/views/Region';
    import styles from './TwoColumnLayout.module.css';
    
    const TwoColumnLayout = (props: LayoutProps) => {
        const regions = props.layout.regions;
        const {common, meta} = props;
    
        return (
            <>
                <div className={styles.row}>
                    <RegionView name="left" components={regions['left']?.components} common={common} meta={meta}/>
                    <RegionView name="right" components={regions['right']?.components} common={common} meta={meta}/>
                </div>
            </>
        );
    };
    
    export default TwoColumnLayout;

    And its CSS module:

    src/components/layouts/TwoColumnLayout.module.css
    .row {
        display: flex;
        gap: 10px;
        margin-bottom: 10px;
        flex-wrap: wrap;
    }
    
    .row :global(div) {
        flex: 1;
        min-width: 200px;
    }
    
    .row :global(.xp-page-editor-region-placeholder) {
        margin-bottom: 0;
    }
    This is an example of using CSS modules. For more details visit the Next.js style documentation.
  2. Register the component - Add these new lines to the _mappings.ts file:

    src/components/_mappings.ts
    import TwoColumnLayout from './layouts/TwoColumnLayout';
    
    
    // Layout mappings
    ComponentRegistry.addLayout(`${APP_NAME}:2-column`, {
        view: TwoColumnLayout
    });
    This specific layout does not need any additional data, hence no query or processor.
  3. Verify the result - In Content Studio, add the layout to a page: drag Layout from the Insert tab into the region and select 2 columns in the Inspect tab. Then add parts inside the left and right regions of the layout.

    By combining layouts and parts, the potential of the page editor starts to show:

    Front page with a paragraph above a two-column layout holding a child list and a paragraph

Summary

You’ve added a 2-column layout with left and right regions, and used it to place components side by side on a page.

Next, you’ll create page templates, which reuse a page definition across multiple content items.


Contents

Contents