Using drag and drop areas to support content creators easily making layout, style and content changes

Last updated:

Drag and drop areas allow developers to create sections of pages that support layout, stylistic and content changes directly within the content editors. This allows developers to create few templates with global structure, that support content creators making a multitude of pages with various purposes and layouts, without ever fumbling with code or requiring new templates for small layout changes. 

a new approach

Developers can specify empty drop zones for drag and drop areas, where content creators build their own page content and layout, or, developers can pre-populate drag and drop areas with various modules, layouts, styles and content to act as a starting point for content creators to work with.

This tutorial will take you through setting up a simple drag and drop area. For more developer resources on drag and drop areas, see the boilerplate for best practices on implementation as well as the drag and drop area HubL tag reference documentation.

1. Create a new HTML template

Create a new html template to house the HubL and HTML which will make up your drag and drop section.

Drag and drop areas are based on a 12 column responsive grid. Drag and drop tags render markup with class names designating columns and rows.  You are responsible for adding a stylesheet to target those class names. An example of layout styles you could implement can be found in: CMS-Theme-Boilerplate. Your stylesheet can be added to the template using {{ require_css() }}.

2. Create a drag and drop area

A dnd_area is the container that makes a portion of the web page editable in terms of its structure, design, and content. The body of a dnd_area tag supplies the default content for the drag-and-drop area. 

This tag on its own will generate a drop zone for content creators to drag modules into within the content creator. 

HubL
{% dnd_area "body_dnd_area" %}
	<!-- generates an empty drag and drop area drop-section -->
{% end_dnd_area %}

3. Create a section with a module

A dnd_section is a top-level row, and can only be a direct child of a dnd_area. Sections support a variety of parameters that control default values for stylistic controls content creators have for sections within the content creators.

Let's set a background image, and set a centered vertical alignment and 1000px max-width for child content. For a full list of supported parameters on the drag and drop HubL tags, see the drag and drop area HubL tag reference documentation. 

To prepopulate the section with content, we can use the dnd_module tag to include a module by referencing its path. In this example, we are referencing a default HubSpot module, but you can additionally include modules you have built, specifying their path within your Design Tools file tree.

To specify a default value for our dnd_module, we can use the module_attribute tag.

HubL
{% dnd_area "body_dnd_area" %}
    {% dnd_section 
        background_image = {
            'backgroundPosition': 'MIDDLE_CENTER',
            'backgroundSize': 'cover',
            'imageUrl': 'https://www.dragndrop.com/bg-image.jpg'
          },
          max_width=1000,
          vertical_alignment='MIDDLE'
    %}
        {% dnd_module path='@hubspot/rich_text' %}
            {% module_attribute "html"%}
                This is your main headline.
                Use this space to tell everyone about what you have to offer.
            {% end_module_attribute %}
        {% end_dnd_module %}
    {% end_dnd_section %}
{% end_dnd_area %}

Now, we can see our drag and drop area contains a module content creators can edit within the content editor. We can also see how setting a max_width on the dnd_section is affecting our content. 

dnd_rich_text

4. Include multiple modules

To include more than one module, we can use multiple dnd_module tags. By setting the offset and width parameters, which are based off of a 12 column grid, we can place an image module next to our rich text module. 

HubL
{% dnd_area "body_dnd_area" %}
    {% dnd_section 
        background_image={
            'backgroundPosition': 'MIDDLE_CENTER',
            'backgroundSize': 'cover',
            'imageUrl': 'https://www.dragndrop.com/bg-image.jpg'
        },
        max_width=1000,
        vertical_alignment='MIDDLE'
    %}
        {% dnd_module path='@hubspot/rich_text', width=8, offset=0, label="Rich Text" %}
            {% module_attribute "html"%}
            	<h1>This is your main headline.</h1>
            	<p>Use this space to tell everyone about what you have to offer.</p>
            {% end_module_attribute %}
        {% end_dnd_module %}
        {% dnd_module path='@hubspot/linked_image', 
          width=4, 
          offset=8, 
          img={
            "src": "https://www.dragndrop.com/placeholder-image.jpg",
            "alt": "Stock placeholder image"
          } 
        %}
        {% end_dnd_module %}
    {% end_dnd_section %}
{% end_dnd_area %}

Now, we also have an editable image module, as well as a drag handle, allowing content creators to change the width and offset of the modules. We can also see how setting a vertical_alignment on the dnd_section is vertically centering our content. 

dnd_rich_image

5. Incorporate columns and rows

To make our drag and drop area more complex, we can incorporate rows and columns using the dnd_row and dnd_column tags. Rows and columns act similarly to sections in the content editor, where content creators can drag them around, as well as clone, delete and style the rows and columns. 

HubL
{% dnd_area "body_dnd_area" %}
  {% dnd_section 
    background_image={
        'backgroundPosition': 'MIDDLE_CENTER',
        'backgroundSize': 'cover',
        'imageUrl': 'https://www.dragndrop.com/bg-image.jpg'
    },
    max_width=1000,
    vertical_alignment='MIDDLE'
  %}
    {% dnd_module path='@hubspot/linked_image', 
      width=6, 
      img={
        "src": "https://www.dragndrop.com/placeholder-image.jpg",
        "alt": "Stock placeholder image"
      }
    %}
    {% end_dnd_module %}
    {% dnd_column width=6, offset=6 %}
      {% dnd_row 
        padding={
            'bottom': 15
        } 
      %}
        {% dnd_module path='@hubspot/rich_text' %}
          {% module_attribute "html"%}
              <h1>This is your main headline.</h1>
              <p>Use this space to tell everyone about what you have to offer.</p>
          {% end_module_attribute %}
        {% end_dnd_module %}
      {% end_dnd_row %}
      {% dnd_row %}
        {% dnd_module path='@hubspot/form' %}
        {% end_dnd_module %}
      {% end_dnd_row %}
    {% end_dnd_column %}
  {% end_dnd_section %}
{% end_dnd_area %}

Now, our content creators will have further stylistic and layout control over specific rows and columns, in addition to modules and sections. 

dnd_area_full

6. Set generic drag and drop component styles

The various components of drag and drop areas, sections, columns, rows and modules all have classes which can be styled using CSS. The editable styles and options for these components can be set using CSS rather than HubL. For example, default padding can be set on dnd_sections with the CSS:

CSS
.dnd-section {
	padding: 80px 20px;
}

The generic CSS selectors for the drag and drop area components are .dnd-section, .dnd-column, .dnd-row and .dnd-module. Aside from these dnd prefixed classes the actual grid class names in the markup are based on bootstrap 2 names. This does not mean you need to use bootstrap 2 with drag and drop areas. When you add a dnd_area to your page, you are responsible for providing the styles that make the grid work. An example of layout styles you could implement can be found in: CMS-Theme-Boilerplate. Your stylesheet can be added to the template using {{ require_css() }}.

For more developer resources on drag and drop areas, see the boilerplate for best practices on implementation as well as the drag and drop area HubL tag reference documentation.