Block Editor

Last Updated: Jan 27, 2023
documentation for the dotCMS Content Management System

The Block Editor field in dotCMS is a new rich content editor that allows you to create your content as building blocks. Every element inside your editor is a block that you can add, edit, drag, and drop to reorder and delete.

Block Types

Block types currently include the following:

BlockSystem Name(s)Description
ParagraphparagraphDefault block type, for text and formatting marks
Headingheading1
heading2

heading6
Various levels of section headings
ListbulletList
orderedList
listItem
Ordered or unordered lists, and the items thereof
Block QuoteblockQuoteFor paragraph-length quotations
Code BlockcodeBlockMonospaced preformatted section
Horizontal LinehorizontalRuleA line for dividing vertical sections
ImagedotImageFully compatible with copy/paste or drag & drop insertion; image blocks are stored as dotAssets
TabletableDisplays data in rows and columns; tables can be adjusted on the fly through the menu tab that appears in the upper-right corner of an active cell
ContentletcontentletsSpecial blocks consisting of Content Type instances defined elsewhere in dotCMS, allowing for the creation of rich and dynamic content

Block Formatting

Apply marks to text with the Block Editor's text menu.

In the user interface, the Block Editor field surfaces a bubble menu any time a selection is made, allowing you to easily designate text as bold, italic, underlined, and struck.

You can alter the alignment of blocks, including inside table cells.

Aligning a table cell through the bubble menu.

Finally, many blocks can be freely transformed into other blocks; a paragraph can be turned into a heading, a blockquote, and so on, through a simple dropdown selection.

Inline Editing in Edit Mode

Aside from the primary Block Editor field interface accessible within a contentlet, these fields can also be edited inline in Edit Mode in version 23.01 or later.

Blocks can be edited inline via Edit Mode.

To make this feature available, ensure the containing element has the following attributes defined:

  • data-language
  • data-inode
  • data-field-name
  • data-content-type
  • data-block-editor-content

These attributes can be added conditionally via Velocity, surfacing only in the context of Edit Mode. Here's an example drawn from the dotCMS Demo site:

<div class="section-blog-post-content"
    #if ($EDIT_MODE) 
        data-language="${CONTENT_LANGUAGE}"
        data-inode="$URLMapContent.inode"
        data-field-name="blogContent"
        data-content-type="Blog"
        data-block-editor-content="$esc.html($!{URLMapContent.blogContent.content})"
    #end >
    $!{URLMapContent.blogContent.toHtml('/application/vtl/blog/blocks/')}
</div>

Field Variables

Screenshot of several example field variable settings.

Styling

When defining a Block Editor field inside a Content Type, you can define its CSS styling by way of a field variable. Simply add a field variable with the key styles and set its value equal to a CSS string containing the desired settings, separated by semicolons.

Limiting Available Blocks

It may be useful or necessary in some cases to limit the blocks that a user can add to a given Block Editor Field. For example, typically the h1 tag is occupied by the page or article title, and thus an h1 is not needed on a given blog post.

To limit the available blocks, create a whitelist with the allowedBlocks field variable, which accepts a comma-separated list of block system names, as detailed in the table above — such as heading2,heading3,paragraph.

This option can now also be set through the traditional settings interface in a tab next to field variables, as of version 23.01:

Whitelisting blocks through the Settings menu.

Limiting Available Contentlets

Similar to whitelisting block types, it is possible to limit the Content Types available for insertion into the Block Editor as contentlets. Use the key contentTypes, and set the value to a comma-separated list of Content Type variables — such as, in the above image, Activity,Blog.

The Count Bar and Character Limits

Starting with version 22.12, Block Editors display character counts, word counts, and estimated reading times under the field. This feature comes with two field variables:

  • displayCountBar, which defaults to true but can be set to false to remove the display, and
  • charLimit, which takes a numeric value to limit the acceptable length of the content by character count.
charLimit not setcharLimit = 5000
An example of the Block Editor without charLimit set.An example of the Block Editor with charLimit set to five thousand.

JSON Object

The block content gets saved as a JSON object with all the information you need to render it in your page. For example, say your Block Editor's content consists of a single paragraph containing “Hello World”; its final object would look like this:

{
  "type": "doc",
  "content": [
    {
      "type": "paragraph",
      "content": [
        {
          "type": "text",
          "marks": [
            {
              "type": "bold"
            }
          ],
          "text": "Hello"
        },
        {
          "type": "text",
          "text": " World"
        }
      ]
    }
  ]
}

Each block object has two important properties:

  1. type: a string with the name of the block
  2. content: an array of blocks containing the content

The JSON output always begins with a type of doc, which is the parent block. The content property contains all the child-block data — i.e., the content itself.

Within the content blocks, there are further conventions to note, the most important of which is the distinction between text and non-text block types.

The text Type

A content type of text specifies the raw text of the block. For example, a heading, paragraph, or blockquote block will contain content of type text. These members may additionally contain a property named marks, which contain formatting information: bold, italic, underline, etc.

Non-Text Types

Other blocks, such as image or contentlet blocks, will store in their content all the properties necessary to render them. For an image block, this includes its path, width, and height; for a contentlet, it will have all the information about the contentlet field, including the iNode, identifier, etc.

Rendering Content

VTL Rendering

To render a Block Editor field from within a container, call the toHtml() velocity method on the Block Editor's field object:

$dotContentMap.{blockEditorVar}.toHtml('/application/storyblock/')

This will render the content of the field into basic HTML.

For example:

  • Paragraphs: <p>Content</p>
  • Headings:
    • <h1>Content</h1>
    • <h2>Content</h2>
    • <h3>Content</h3>
  • List: <ul><li>Content</li></ul>
    • Ordered <ol><li>Content</li></ol>
    • Unordered <ul><li>Content</li></ul>
  • Horizontal Line: <hr>

Customizing Block or Contentlet Rendering

dotCMS permits customization of how blocks are rendered as HTML. Simply create a VTL file named for a block you wish to override, and then define the rendering behavior within. These files should live in /application/storyblock, though you may also add further subfolders if more than one set of rendering customizations are needed.

To override the rendering behavior for a contentlet, the VTL file name must begin with dotContent-. For example, in the case of the two Content Types used as examples in the field variables section, you would use files named dotContent-Activity.vtl and dotContent-Blog.vtl.

For a list of valid block names for use in custom VTL definition, see the “System Name” column of the table under Block Types). Additionally, there are two other overridable type names:

TypePurpose
textDescribed under The text Type
hardBreakAllows the override of line-break behavior

Finally, call the toHtml() method, passing as an argument the path to the folder containing the overriding file or files.

Customization Example 1: Heading 1

To modify the Heading 1 blocks, first create a file named heading1.vtl inside the /application/storyblock folder.

Add the following code to the file:

#parse( "static/storyblock/render.vtl" )
<h1 class="text-heading">
    #renderContentBlock($item.content)
</h1>

In this code:

  1. We import the macros.
  2. We add the HTML <h1> tag with any class or attributes we need.
  3. Render the text using the renderContentBlock macro that will handle formatting marks automatically.

Finally, update your container code to pass the folder with the VTL you created to the toHtml() method like this (replacing blockField with the field's actual variable):

$dotContentMap.blockField.toHtml('/application/storyblock/')

dotCMS will look for VTL files for each block in the storyblock folder and fall back to the default rendering behavior if no custom file is found. If you had used a subfolder within /application/storyblock/, be sure to specify it in the toHtml() function call.

Customization Example 2: Table

To create a custom table block, create a table.vtl file under /application/storyblock/ with the following content:

#parse( "static/storyblock/render.vtl" )

<table class="custom-class">
 #foreach($row in $item.content)
  <tr>
     #foreach($cell in $row.content)
        #if( $cell.type == "tableHeader" )
            <th colspan="$!cell.attrs.colspan" rowspan="$!cell.attrs.rowspan" colwidth="$!cell.attrs.rowspan" >#renderContentBlock($!cell.content)</th>
        #elseif($cell.type == "tableCell" )
            <td colspan="$!cell.attrs.colspan" rowspan="$!cell.attrs.rowspan" colwidth="$!cell.attrs.rowspan">#renderContentBlock($!cell.content)</td>
        #end
     #end
  </tr>
 #end
</table>

As with the Heading 1 example, you'll need to make sure the container controlling the display of the Block Editor contains the necessary .toHtml('/application/storyblock/') method. However, if you've already implemented it, such as through the Heading 1 example, then there's no need to add it a second time.

Customization Example 3: Styling Blocks

Using the tools above, there are a number of ways to manage the styling of blocks. One is to add CSS classes through the custom-block VTLs, as above. An even simpler option is to apply classes via containers.

This can require as little as wrapping the .toHtml() call in a <div> block with a custom class. Using the $dotContentMap built-in content object and assuming the Block Editor field has a variable name of blockField, this might look like the following:

<div class="my-custom-content">
  $dotContentMap.blockField.toHtml('/application/storyblock/')
</div>

Then define styles in CSS based on descendents of the custom class:

.my-custom-content h1 {
    // h1 block styles
}

.my-custom-content h2 {
    // h2 block styles
}

.my-custom-content p {
    // paragraph block styles
}

It is not recommended to add <style> tags directly to the rendering VTL files, as this can generate redundant code.

Headless Rendering

Headless rendering of the Block Editor begins with an API call to fetch the JSON object. This call may be made via a variety of APIs, including the Page API, GraphQL, Content API, Elasticsearch API, or any other API capable of calling a contentlet containing a Block Editor field.

For example, a Page API call would be a straightforward retrieval using this address template:

https://{server_address}/api/v1/page/json/{page_path}

Below is a demo page and its corresponding Page API path. Please note that the Page API requires being signed in (user:admin@dotcms.com, password:admin):

…/blog/post/french-polynesia-everything-you-need-to-know /api/v1/page/json/blog/post/french-polynesia-everything-you-need-to-know

On the linked page's output, the blogContent field begins as all Block Editor JSON objects do: with "type": "doc". Its second member, content, contains a nested array of all the child blocks — i.e., everything entered in the Block Editor.

Once the JSON object is parsed, your options for rendering are virtually limitless. Grab a Javascript framework of your choice and get creative!

Here's one compact example of rendering blocks using React.

Embedded Contentlets

As noted above, contentlets of other Content Types can be embedded for display within a Block Editor. Through field variables, you can limit which Content Types it will accept, and through custom VTL files you can control how they will render within the field.

In versions prior to 22.10, contentlets in a Block Editor are stored as copies in the field's JSON data; if the original contentlet undergoes changes, the version stored in the Block Editor will not automatically update, and requires manual re-insertion to remain up to date.

As of version 22.10, Block Editor contentlets remain automatically up to date with their originals, syncing up with both the database and the cache. It should be noted that this operation carries a slight performance cost — in most cases statistically insignificant. Nevertheless, this cost can be offset by disabling the cache synchronization by setting the following environment variable:

DOT_REFRESH_BLOCK_EDITOR_REFERENCES=false

Converting WYSIWYG Fields

As of version 22.10, it is now possible to transform WYSIWYG fields into Block Editor fields within a given Content Type. For more information, see Converting WYSIWYG to Block Editor.

On this page