Regions

Contents

In this chapter, you’ll add a region to the page component, so pages can be composed from several components in Content Studio.

Component dropzones

A region is a named area of a page where editors can add components. For Content Studio to offer this, the page component must declare one or more regions.

Task: Add a region to the page

Add a single region called main to the hello page component, using the code samples that came with the starter.

  1. Update the descriptor - Replace the content of the page component descriptor with the following, which declares the region:

    src/main/resources/site/pages/hello/hello.xml
    <page xmlns="urn:enonic:xp:model:1.0">
      <display-name>Hello Regions!</display-name>
      <description>Drag'n drop enabled</description>
      <form/>
      <regions>
        <region name="main"/>
      </regions>
    </page>
  2. Update the controller - Replace the content of the controller with this version, which passes the region to the view:

    src/main/resources/site/pages/hello/hello.ts
    import type { Response } from '/index.d';
    
    // @ts-expect-error no-types
    import {render} from '/lib/thymeleaf';
    import {getContent} from '/lib/xp/portal';
    
    // Specify the view file to use
    const VIEW = resolve('./hello.html');
    
    // Handle the GET request
    export function get(): Response {
    
        // Get the content that is using the page
        const content = getContent();
    
        // Prepare the model that will be passed to the view
        const model = {
          displayName: content.displayName,
          mainRegion: content.page.regions.main
        }
    
        // Prepare the response object
        const response: Response = {
    		  body: render(VIEW, model)
    	  };
    
        return response;
    }
  3. Update the view - Replace the content of the view with this version, which renders the region:

    src/main/resources/site/pages/hello/hello.html
    <!DOCTYPE html>
    <html>
      <head>
        <title data-th-text="${displayName}">Sample title</title>
      </head>
      <body>
        <header>
          <h1 data-th-text="${displayName} + ' - with regions'">Sample header</h1>
        </header>
        <main data-portal-region="main">
            <div data-th-if="${mainRegion}" data-th-each="component : ${mainRegion.components}" data-th-remove="tag">
                <div data-portal-component="${component.path}" data-th-remove="tag"></div>
            </div>
        </main>
        <footer>My first site - powered by Enonic</footer>  
      </body>
    </html>

    Your sandbox picks up the changes automatically, since enonic dev is running.

  4. Open a country - In Content Studio, select one of the country content items and click Edit.

  5. Select the page component - Activate the page editor with the monitor icon in the top right corner, and select the Hello Regions! page component.

  6. Verify the region - The page now contains an empty region, marked Drop here:

    Setting up a page with a region

Text component

The Text component is a standard component included in XP. It comes with a default rendering, which you can customize if needed.

Task: Add text to the region

  1. Insert a text component - Drag Text from the Components widget on the right-hand side into the region. You can also right-click the region and select Insert → Text.

  2. Write some text - Type a text, for example "Hello Regions!", and save the content.

  3. Verify the result - The text is rendered inside the region, below the page heading.

For more details, visit the reference documentation for the Text component.

Summary

You’ve declared a region in the page component, and filled it with a text component from Content Studio. Regions aren’t limited to pages. They are also used in layouts, a component type you’ll meet in the layouts chapter.

To learn more about regions, visit the regions documentation.

Next, you’ll add parts to the region. Parts are components you write the rendering for yourself.


Contents

Contents