Layout components

Contents

In this chapter, you’ll use a layout to arrange components in two columns, and see how a component can contribute CSS to the page it is placed on.

Two-column layout

Layouts are components that contain regions, which makes them containers for other components. Your app includes a layout with two regions that work as columns, and a configuration option that lets editors control the width of each column.

Like pages and parts, a layout consists of a descriptor, a controller, and a view. Layouts are placed in the src/main/resources/site/layouts/ folder:

src/main/resources/site/layouts/two-columns/two-columns.xml
<layout xmlns="urn:enonic:xp:model:1.0">
  <display-name>Two columns</display-name>
  <description>A layout with configurable two columns</description>
  <form>
    <input type="ComboBox" name="sizes">
        <label>Column sizes</label> 
        <occurrences minimum="1" maximum="1"/>
        <config>
            <option value="33_66">33/66</option>
            <option value="50_50">50/50</option>
            <option value="66_33">66/33</option>
        </config>
        <default>50_50</default>
    </input>
  </form>
  <regions>
    <region name="left"/>
    <region name="right"/>
  </regions>
</layout>

The descriptor combines what you’ve seen in pages and parts: it declares regions, and it has a form.

src/main/resources/site/layouts/two-columns/two-columns.ts
import { LayoutComponent, LayoutRegion } from '@enonic-types/core';
import type { Response } from '/index.d';

// @ts-expect-error no-types
import {render} from '/lib/thymeleaf';
import {assetUrl, getComponent} from '/lib/xp/portal';

type LayoutDescriptor = "company.starter.myfirstsite:two-column"
type LayoutConfig = { sizes: '33_66' | '50_50' | '66_33' }
type Layout = LayoutComponent< LayoutDescriptor,LayoutConfig, {left: LayoutRegion, right: LayoutRegion} >

// Specify the view file to use
const VIEW = resolve('./two-columns.html');

// Handle the GET request
export function get(): Response {

  // Get the layout component
  const component = getComponent<Layout>();

  // Prepare the model that will be passed to the view
  const model = {
    sizes: component.config.sizes,
    leftRegion: component.regions.left,
    rightRegion: component.regions.right
  }

  // Prepare the response object
  const response: Response = {
    body: render(VIEW, model),
    pageContributions: {
      
      headEnd: [
        `<link rel="stylesheet" href="${assetUrl({ path: 'styles/two-columns.css' })}" type="text/css" />`
      ]
    }
  };

  
  return response;
}
src/main/resources/site/layouts/two-columns/two-columns.html
<section data-th-class="'two-columns two-columns--' + ${sizes}">
    <div data-portal-region="left">
        <div data-th-if="${leftRegion}" data-th-each="component : ${leftRegion.components}" data-th-remove="tag">
            <div data-portal-component="${component.path}" data-th-remove="tag"></div>
        </div>
    </div>
    <div data-portal-region="right">
        <div data-th-if="${rightRegion}" data-th-each="component : ${rightRegion.components}" data-th-remove="tag">
            <div data-portal-component="${component.path}" data-th-remove="tag"></div>
        </div>
    </div>    
</section>

Page contributions

The layout comes with CSS that is only loaded on pages where the layout is used. The stylesheet is placed in the assets folder:

src/main/resources/assets/styles/two-columns.scss
.two-columns {
    display: grid;
    gap: 2rem;

    &--33_66 {
        grid-template-columns: calc(33.3% - 1rem) auto;
    }

    &--50_50 {
        grid-template-columns: repeat(2, calc(50% - 1rem));
    }
    
    &--66_33 {
        grid-template-columns: calc(66.6% - 2rem) auto;
    }
}
The build system compiles the scss file to css.

To add the CSS to the page, the controller uses page contributions. A page contribution lets a component inject HTML at specific places in the resulting markup: at the beginning or end of <head>, or at the beginning or end of <body>.

The controller above returns the page contribution together with the rest of its response.

Task: Add the layout to a page

  1. Open the site - In Content Studio, select the Hello World site and click Edit.

  2. Insert the layout - Insert a layout in the main region, and select Two columns in the list:

    Two-column layout with the 50/50 setting and empty left and right regions
  3. Add text to the columns - Add a text component to each of the two regions, and write some text:

    Two-column layout with the 50/50 setting and a text in each region
  4. Change the column sizes - With the layout selected, switch between 33/66, 50/50, and 66/33 in the Column sizes dropdown in the panel on the right-hand side.

  5. Verify the result - The width of the columns follows the setting you select:

    Two-column layout with the 33/66 setting and a text in each region

Summary

You’ve added a layout with two regions to a page, filled its columns, and changed its configuration. By combining content types, pages, parts, layouts, and page templates, you have the building blocks for a complete site.

For more details, visit the layouts documentation.

Next, you’ll publish the site.


Contents

Contents