Skip the setup. Our team will have you taking orders in 24 hours. Get started →
Customize

Page Builder

Introduction Using the page builder Locked themes Adding page builder support to a theme Rendering blocks on a page Declaring buildable pages The sectio...

The page builder lets site owners arrange the content of theme pages from the admin area, without touching template files. A page is made of sections stacked on top of each other. Each section has a column layout, and each column holds one or more blocks, such as a heading, an image or the menu item list.

The page builder replaces the theme template editor. Template markup and PHP code can no longer be edited from the admin area: theme files are edited with a code editor, and the page builder changes only how blocks are arranged and configured on the pages a theme makes available to it.

This article explains how to use the page builder and how to add page builder support to a theme: declaring buildable pages, rendering sections, and creating blocks.

Open the page builder from the Design > Themes admin page by clicking the Page builder button next to a theme. The button is also shown on the theme customisation page. It is only offered for themes that have at least one buildable page.

The builder lists the theme's buildable pages beside the sections of the page being edited. From here you can:

  • Add a section, choose its column layout, drag it to reorder, duplicate or remove it.
  • Edit the section settings: width (boxed or full width), vertical spacing, background colour, background image, anchor ID, CSS class and HTML attributes.
  • Add blocks to a column, drag them between columns and sections, edit their settings or remove them.
  • Reset to theme default to discard your changes to a page and return to the layout the theme ships with.
  • Add a new page, then rename or delete it later. New pages are created as page files in the theme, so the theme must not be locked and must have at least one layout.
  • Edit the page settings: title, permalink, layout, description and whether the page is hidden.

Some rules are enforced while editing:

  • A required block cannot be removed from its page. For example, a checkout page always keeps its checkout block. A required block that is missing is added back in a new section at the end of the page.
  • A unique block can only be used once on a page.
  • A wide block can only be placed in a column that spans at least half the row.
  • A block limited to certain pages is only offered on those pages.

The sections of a page are saved to the database, so pages of a locked theme can still be rearranged. Page files of a locked theme cannot be changed, as updating the theme would replace them. When you open the page settings on a locked theme, the builder offers to create a child theme, which uses the parent theme's pages and keeps your changes through updates. Adding, renaming and deleting pages is likewise only available on a theme that is not locked.

A theme supports the page builder when it provides three things: pages that render their blocks with the @themeBlocks directive, a section view, and a view for each of its blocks.

acme/
  purple/
    resources/
      meta/
        builder.php              <=== Declares buildable pages and their default sections
      views/
        _builder/
          section.blade.php      <=== Renders a section and its columns
          blocks/
            heading.blade.php    <=== Renders the "heading" block
        _pages/
          home.blade.php         <=== Renders its sections with @themeBlocks
    src/
      Blocks/
        Heading.php              <=== Block class

Use the @themeBlocks directive in a page template to render the sections saved for that page:


---

title: Homepage
permalink: /
layout: default

---

@themeBlocks

You can render markup around the directive, for example a fixed page header above the blocks. A page whose markup is only @themeBlocks is treated as a page made with the page builder, and is listed in the builder even when it is not declared in builder.php.

The resources/meta/builder.php file lists the pages that can be edited with the page builder, keyed by page code. The page code is the page file name relative to the _pages directory, with subdirectories separated by a dot. For example, _pages/local/menus.blade.php has the code local.menus.

<?php

return [
    'pages' => [
        'home' => [
            'label' => 'acme.purple::default.home_title',
            'default' => [
                [
                    'id' => 's_home_intro',
                    'layout' => 'cols-1',
                    'settings' => ['container' => 'boxed', 'spacing' => 'lg'],
                    'columns' => [
                        ['blocks' => [
                            ['id' => 'b_home_heading', 'code' => 'heading', 'settings' => ['title' => 'Welcome']],
                        ]],
                    ],
                ],
            ],
        ],
        'local.menus' => [
            'label' => 'acme.purple::default.menus_title',
            'required' => ['menu-item-list'],
            'default' => [
                [
                    'id' => 's_menus_items',
                    'layout' => 'sidebar-right-4',
                    'columns' => [
                        ['blocks' => [['id' => 'b_menus_items', 'code' => 'menu-item-list']]],
                        ['blocks' => [['id' => 'b_menus_cart', 'code' => 'cart-box']]],
                    ],
                ],
            ],
        ],
    ],
];

The following options are available for each page:

Option Description
label The page name shown in the builder's pages list, can be a language key. Defaults to the page code.
required A list of block codes that must be present on the page, optional. Required blocks cannot be removed in the builder and are added back if missing.
default The sections the page renders until it is changed in the builder, and returns to when it is reset, optional.

Each section in default has the following structure:

Key Description
id A unique identifier for the section, letters, numbers, dashes and underscores only. One is generated when left out.
layout The section layout code. Unknown layouts fall back to cols-1.
settings The section settings: container, spacing, background_color, background_image, anchor, css_class and html_attributes.
columns One entry per column of the layout, each with a blocks list. A block has an id, the block code and its settings.

Make each page default reproduce the markup the page used before it was made buildable. The page then looks the same until someone changes it in the builder.

Sections are cleaned every time they are loaded. Blocks that are not registered, not allowed on the page, or placed in a column too narrow for them are dropped, and settings a block does not define are removed.

Sections are rendered with the theme's _builder/section.blade.php view. It receives the $section being rendered and a $renderBlock closure that returns the HTML of a block:

@php
    $settings = $section['settings'];
    $attributes = $section['attributes'];
    $spacing = ['none' => '', 'sm' => 'py-3', 'md' => 'py-4', 'lg' => 'py-5'][$settings['spacing']];
@endphp
<section
    id="{{ $attributes['id'] ?? ($settings['anchor'] ?: $section['id']) }}"
    @class(['page-section', $spacing, $settings['css_class']])
    {!! \Igniter\Main\Classes\HtmlAttributes::toHtml(array_except($attributes, ['id', 'class'])) !!}
>
    <div @class(['container' => $settings['container'] === 'boxed'])>
        <div class="row">
            @foreach($section['columns'] as $column)
                <div @class([
                    'col-12',
                    'col-lg' => $column['span'] === null,
                    'col-lg-'.$column['span'] => $column['span'] !== null,
                ])>
                    @foreach($column['blocks'] as $block)
                        @if($block['attributes'])
                            <div {!! \Igniter\Main\Classes\HtmlAttributes::toHtml($block['attributes']) !!}>
                                {!! $renderBlock($block) !!}
                            </div>
                        @else
                            {!! $renderBlock($block) !!}
                        @endif
                    @endforeach
                </div>
            @endforeach
        </div>
    </div>
</section>

The $section array contains:

Key Description
id The section identifier.
layout The section layout code.
equal true when the layout splits the row into equal columns.
settings The section settings. container is boxed or full, and spacing is none, sm, md or lg.
attributes The HTML attributes entered for the section, as a name and value array.
columns The columns. Each has a span on a 12 column grid and its blocks. The span is null for equal layouts that do not divide 12 evenly, use an equal width grid there.

Each block in a column has its id, code, settings, the data returned by the block's resolve method, and attributes holding the CSS class and HTML attributes entered for it.

Every section and block has a CSS class and an HTML attributes setting. Attributes are entered one per line, such as data-track="hero". Only id, title, role, lang, dir, tabindex, style, hidden and any data-* or aria-* attribute are kept. Event handlers, links and values containing script URLs are dropped.

Blocks do not render these settings themselves. They are passed to the section view in $block['attributes'], which should place them on an element around the block, as shown above. Use Igniter\Main\Classes\HtmlAttributes::toHtml() to render an attributes array, it escapes every name and value.

A block is a class that extends Igniter\Main\Classes\BaseBlock. It describes the block, defines the form fields used to edit its settings, and is rendered with a view in the theme.

namespace Acme\Purple\Blocks;

use Igniter\Main\Classes\BaseBlock;

class Heading extends BaseBlock
{
    public function blockDetails(): array
    {
        return [
            'code' => 'heading',
            'name' => 'acme.purple::default.block_heading',
            'description' => 'acme.purple::default.block_heading_desc',
            'icon' => 'fa fa-heading',
        ];
    }

    public function defineFields(): array
    {
        return [
            'title' => [
                'label' => 'Title',
                'type' => 'text',
                'validationRule' => ['required', 'string', 'max:255'],
            ],
            'level' => [
                'label' => 'Heading level',
                'type' => 'radiotoggle',
                'default' => 'h2',
                'options' => ['h1' => 'H1', 'h2' => 'H2', 'h3' => 'H3'],
                'validationRule' => ['in:h1,h2,h3'],
            ],
        ];
    }
}

The blockDetails method returns the following details:

Key Description
code A unique code for the block, required. It is also the name of the block view.
name The block name shown in the builder, can be a language key.
description A short description shown in the builder, optional.
icon The icon CSS class, optional. Defaults to fa fa-cube.
unique When true, the block can only be used once on a page, optional.
wide When true, the block can only be placed in a column spanning at least 6 of the 12 grid columns, optional.
pages A list of page codes the block may be used on, optional. Blocks without this option are available on all pages.

The defineFields method returns form field definitions keyed by setting name. The default of each field is used when the block is added to a page, and the validationRule is applied when the block settings are saved in the builder.

A block is rendered with the theme's _builder/blocks/{code}.blade.php view. The view receives the block $settings, the $data returned by the block's resolve method, and the whole $block:

{{-- resources/views/_builder/blocks/heading.blade.php --}}
@php($tag = in_array($settings['level'], ['h1', 'h2', 'h3'], true) ? $settings['level'] : 'h2')
<{{ $tag }}>{{ $settings['title'] }}</{{ $tag }}>

Block settings are entered by admin users. Always escape them with {{ }}, and validate values used in tag names, class names or URLs as the example does.

You may override these methods to change how a block behaves:

Method Description
defaults() The settings of a newly added block. Defaults to the default of each field.
resolve(array $settings) Returns the data passed to the block view as $data. Use it to load records or build URLs from the settings.
sanitize(array $settings) Cleans the settings before they are saved or rendered. Only keys the block defines are kept. Override it to purify HTML settings, calling the parent first.
summary(array $settings) The short text shown on the block in the builder. Defaults to the first non-empty text setting.
render(string $viewNamespace, array $block) Renders the block. Override it to render something other than the _builder/blocks/{code} view.

For example, an image block can resolve the URL of the selected media file:

public function resolve(array $settings): array
{
    return array_merge($settings, [
        'url' => $settings['image'] ? media_url($settings['image']) : null,
    ]);
}

Blocks are registered per theme with the Igniter\Main\Classes\BlockManager class. A theme registers its blocks from the boot method of its service provider. Pass a list of block class names or instances, or a closure returning such a list:

use Acme\Purple\Blocks\Heading;
use Acme\Purple\Blocks\Image;
use Igniter\Main\Classes\BlockManager;

public function boot(): void
{
    resolve(BlockManager::class)->registerBlocks('acme-purple', fn(): array => [
        Heading::class,
        Image::class,
    ]);
}

Extensions register blocks by overriding the registerBlocks method of the Extension class. The blocks are keyed by theme code, use * to make a block available to every theme:

public function registerBlocks(): array
{
    return [
        '*' => [\Acme\HelloWorld\Blocks\Announcement::class],
        'acme-purple' => [\Acme\HelloWorld\Blocks\PurpleBanner::class],
    ];
}

When the same block code is registered more than once, blocks registered for the theme replace those of its parent theme, which replace those registered for all themes.

A block is rendered from a view in the theme. An extension that registers a block for every theme should override the block's render method to render a view of its own.

A component can be offered as a block without writing a block class. Igniter\Main\Classes\ComponentBlock wraps a Livewire or Blade component: the block's name and description come from the component, its settings form is built from the component's defineProperties method, and the component is rendered with the block settings as its properties.

use Igniter\Main\Classes\BlockManager;
use Igniter\Main\Classes\ComponentBlock;

resolve(BlockManager::class)->registerBlocks('acme-purple', fn(): array => [
    ComponentBlock::make('acme-purple::featured-items'),
    ComponentBlock::make('acme-purple::checkout', ['unique' => true, 'wide' => true]),
]);

The first argument is the component code. The second is an array of block details that override those taken from the component. The block code defaults to the part of the component code after ::, so acme-purple::checkout becomes the checkout block.

Component blocks need no block view, as the component renders itself.

A layout sets how many columns a section has and how wide they are. These layouts are built in:

Code Columns
cols-1 to cols-6 1 to 6 equal columns.
sidebar-left-2, sidebar-left-3, sidebar-left-4 A left sidebar and wide content, spanning 2/10, 3/9 or 4/8.
sidebar-right-2, sidebar-right-3, sidebar-right-4 Wide content and a right sidebar, spanning 10/2, 9/3 or 8/4.
center-2, center-3 A wide centre column between two narrow ones, 2/8/2 or 3/6/3.

Extensions add layouts by overriding the registerSectionLayouts method of the Extension class. A layout is a list of column spans on a 12 column grid that must add up to 12:

public function registerSectionLayouts(): array
{
    return [
        'sidebar-left-5' => [
            'label' => 'acme.helloworld::default.layout_5_7',
            'spans' => [5, 7],
        ],
    ];
}

Themes and other packages can call SectionLayouts::register instead:

use Igniter\Main\Classes\SectionLayouts;

SectionLayouts::register('sidebar-left-5', 'acme.purple::default.layout_5_7', [5, 7]);

Built-in layouts cannot be replaced. A section view that lays out columns from their span needs no changes to support a new layout.

A child theme uses the page builder support of its parent. Its builder.php is merged over the parent's by page code, so a child theme only declares the pages it adds or changes. Blocks registered for the parent theme are available to the child theme, and block and section views are rendered from the parent theme.

The page builder is also available as the pagebuilder form widget, so extensions can use sections and blocks for their own content. The Pages extension uses it to offer the page builder as an editor for static pages.

'sections' => [
    'type' => 'pagebuilder',
    'pageCode' => 'static',
],

The field value is the sections array, so cast the model attribute to array. Render the saved sections with the Igniter\Main\Classes\PageBuilder class:

use Igniter\Main\Classes\PageBuilder;

$builder = resolve(PageBuilder::class);
$theme = controller()->getTheme();

$html = $builder->renderBlade(
    $theme,
    $builder->normalize($theme, $model->sections ?? [], 'static'),
    'static',
);

The page code passed to the widget and to the PageBuilder methods decides which blocks are available: blocks limited to other pages with the pages detail are left out.

Did this answer your question?

Your feedback helps us improve our help articles.

See a mistake? Edit this page on GitHub

Need more help?

Explore the help center, join the community, or get priority support from the TastyIgniter team.