Table of Contents

Grid rows and row templates

How row definitions, row settings, and columns reach your row templates, and how to write row templates for a grid-based design

In a grid-based design, editors build pages from rows. Each row has one or more columns, and each column holds one paragraph. You decide which kinds of rows exist and how they are rendered; editors decide which rows a page has, in which order, and what goes in each column.

This article explains how a row gets from the editor to your markup: the row definition you write, the settings editors set on each row, and the GridRowViewModel your row template receives. It assumes you have set up a grid as described in Content models.

The parts of a grid

A grid involves four things, two you create and two DynamicWeb creates for you.

You create:

  • A grid in a layout, with @Model.Grid("Grid", "Grid", "", "Page"); the last argument is the grid type, which points to the Grid/Page/ folder in the design
  • Row definitions in Grid/Page/, each describing a kind of row, e.g. 1 column, 2 columns, or Image with text, and which row template renders it

DynamicWeb creates:

  • Rows on a page whenever an editor adds one in the Visual Editor, each based on a row definition and with its own settings
  • A GridRowViewModel for each row when the page is rendered, which your row template receives as Model

The folder structure looks like this:

/Files/Templates/Designs/MyDesign/
├── Grid/
│   ├── Page/
│   │   ├── RowDefinitions/
│   │   │   ├── 1Column.json
│   │   │   ├── 2Columns.json
│   │   │   ├── 3Columns.json
│   │   ├── RowTemplates/
│   │   │   ├── Row.cshtml

Several row definitions can share one row template. A template that loops over Model.Columns can render rows with any number of columns, which is how Swift renders all its rows with two templates.

DynamicWeb looks for the grid type folder in the design first (Designs/MyDesign/Grid/Page) and then in Designs/Grid/Page, shared by all designs. Only if neither folder exists does it look next to the layout (Designs/MyDesign/{LayoutName}/Grid/Page), and there it only finds a RowDefinitions.json file, not a RowDefinitions folder. Keep the Grid folder in the design, and use a separate grid type, e.g. Grid/Email, when a layout needs its own rows.

Row definitions

A row definition is a JSON object. You can write it by hand, or create it in the administration under Settings > Areas > Content > Grid, which writes a JSON file to the RowDefinitions folder for you. See Grid in the Manual for the administration UI.

Here is a definition for a two-column row with most features enabled:

{
  "Id": "2Columns",
  "Name": "2 columns",
  "Description": "Two columns of equal width",
  "Template": "Row.cshtml",
  "ColumnCount": 2,
  "WidthType": 0,
  "ColumnWidths": [ 6, 6 ],
  "ItemType": "MyDesign_RowSettings",
  "Thumbnail": "/Files/Templates/Designs/MyDesign/Assets/images/rows/2columns.svg",
  "EnableTopSpacing": true,
  "EnableBottomSpacing": true,
  "TopSpacingDefaultValue": 3,
  "BottomSpacingDefaultValue": 3,
  "EnableContainerWidth": true,
  "ContainerWidthDefaultValue": 3,
  "EnableGapSettings": true,
  "GapSettingsDefaultValue": "3,3",
  "EnableVerticalAlignment": true,
  "EnableBackgroundImage": true,
  "EnableDesktopLayout": true,
  "EnableMobileLayout": true,
  "EnableMobileSort": true
}

The core properties describe the row:

Property Use
Id Stored on every row created from the definition. Do not change it once rows exist
Name, Description, Thumbnail Shown to editors when they pick a row type
Template The row template in RowTemplates/
ColumnCount The number of columns, and so the number of paragraphs the row can hold
WidthType 0 (fixed) or 1 (flexible), see Column widths
ColumnWidths The width of each column on a 12-column scale, e.g. [ 8, 4 ]. If left out, columns are equal for 1, 2, 3, 4, and 6 columns
ItemType An item type with extra settings editors fill out on the row

The Enable... properties are row features. Each one adds a setting to the row in the Visual Editor, and the matching ...DefaultValue is used for new rows. A feature does nothing visible by itself. It only gives editors a setting, and your row template decides what the value means in CSS.

Row settings in the template

The row template inherits the GridRowViewModel. The values editors set through the row features are properties on the model:

Feature Property Values editors can choose
EnableTopSpacing TopSpacing 0 (none) to 6: none, XS, S, M, L, XL, and XXL
EnableBottomSpacing BottomSpacing Same as top spacing
EnableContainerWidth ContainerWidth 1 Readability, 2 Small, 3 Regular, or 4 Full (edge to edge)
EnableGapSettings GapX, GapY Off (0) or on (3); GapX is between columns, GapY between rows of wrapped columns
EnableVerticalAlignment VerticalAlignment Start, Center, or End (None when not set)
EnableBackgroundImage BackgroundImage An ImageFileViewModel, or null

All of these are nullable or have a "not set" value. Rows created before you enabled a feature have no value for it, so always fall back to a default in the template, e.g. @(Model.TopSpacing ?? 3).

The model also gives you:

  • Definition: the row definition, e.g. Model.Definition?.Id to add a CSS hook per row type
  • Item: the row settings item if the definition has an ItemType, read like any other item
  • ColorScheme: the color scheme selected on the row, if the website uses color schemes
  • Id: the ID of the row

DynamicWeb only renders rows that are active, within their publication period, allowed for the current user, and not hidden for the current device. You do not need to check this in the template.

Columns

Each column is a GridColumnViewModel. Output() returns the rendered paragraph in the column, using the paragraph template that applies to it. An empty column returns an empty string.

You can address columns by number with Model.Column(1), or loop over Model.Columns. Looping is usually better, because one template then works for any column count:

@foreach (var column in Model.Columns)
{
    <div class="col-12 col-md-@column.ColumnWidth">
        @column.Output()
    </div>
}

Model.Column(n) throws an exception if n is larger than the column count of the row, so only use it in templates made for one specific row definition.

column.Paragraph gives you information about the paragraph in the column, e.g. Paragraph?.ID for an anchor or Paragraph?.Item?.SystemName for a CSS hook per paragraph type. It is null for an empty column.

Column widths

Columns have a width on a 12-column scale, like Bootstrap. A row definition uses one of two width types.

With fixed widths (WidthType: 0), each column has a width from ColumnWidths, available as column.ColumnWidth. Editors can also choose a mobile layout and a mobile order if EnableMobileLayout and EnableMobileSort are set:

  • column.ColumnWidthMobile is the width on small screens, e.g. 12 for stacked or 6 for side by side
  • column.ColumnNumberMobile is the position of the column on small screens, which you can use for CSS order

If the editor has not set a mobile layout, DynamicWeb uses defaults. Two-column rows stack (12) and reverse the order, so the second column comes first on mobile, and three-column rows put the first column last. If you do not want this, ignore ColumnNumberMobile in your template.

A fixed-width row template with mobile settings could look like this:

@foreach (var column in Model.Columns)
{
    <div class="col-@(column.ColumnWidthMobile ?? 12) col-md-@column.ColumnWidth order-@(column.ColumnNumberMobile ?? column.ColumnNumber) order-md-0">
        @column.Output()
    </div>
}

With flexible widths (WidthType: 1), columns share the available space instead of using a fixed width, and editors can mark individual columns as fit to content. In the template, column.FitToContent is true for those columns. This suits rows with buttons, icons, or other content of varying size. Mobile layout and mobile sort are not available for flexible rows.

A complete row template

The template below renders any fixed-width row and uses all the row features. The mapping from setting values to CSS classes is yours to choose; here the values map to Bootstrap spacing utilities.

@inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.GridRowViewModel>
@using Dynamicweb.Frontend

@{
    int top = Model.TopSpacing ?? 3;
    int bottom = Model.BottomSpacing ?? 3;
    int gap = Model.GapX ?? 3;

    string container = (Model.ContainerWidth ?? 3) switch
    {
        1 => "container container-readability",
        2 => "container container-small",
        4 => "container-fluid",
        _ => "container"
    };

    string align = Model.VerticalAlignment switch
    {
        Dynamicweb.Content.Styles.VerticalAlignment.Center => "align-items-center",
        Dynamicweb.Content.Styles.VerticalAlignment.End => "align-items-end",
        _ => ""
    };
}

<section id="row-@Model.Id"
         class="row-@Model.Definition?.Id pt-@top pb-@bottom"
         style="@Model.BackgroundImage?.ToBackgroundStyle(new GetImageSettings { Width = 1920 })"
         data-color-scheme="@Model.ColorScheme?.Id">
    <div class="@container">
        <div class="row g-@gap @align">
            @foreach (var column in Model.Columns)
            {
                <div class="col-@(column.ColumnWidthMobile ?? 12) col-md-@column.ColumnWidth">
                    @column.Output()
                </div>
            }
        </div>
    </div>
</section>

Swift's row templates follow the same idea but write the setting values to data- attributes and map them to CSS in the stylesheet, e.g. data-dw-row-space-top="@(Model.TopSpacing ?? 1)". That keeps the template small and puts the design decisions in CSS. See Grid/Page/RowTemplates/Swift-v2_RowFlex.cshtml in Swift for an example.

Rendering a grid from another page

RenderGrid(pageId) renders the grid of another page inside the current template. It is available in all ViewModel templates and is how Swift builds headers and footers: editors maintain the header as a normal page with rows, and the master template renders it on every page.

@* In Master.cshtml *@
@if (Model.Area?.Item?.TryGetLink("Header", out LinkViewModel header) == true)
{
    <header>
        @RenderGrid(header.PageId)
    </header>
}

Here the header page is selected in a link field on the website settings item, so editors can change it without touching the templates. On multilingual websites, RenderGrid uses the language version of the page that matches the current website.

RenderGridRow(rowId) does the same for a single row.

Troubleshooting

When a row does not render as expected, these are the usual causes:

  • The row type is missing in the Visual Editor: check that Grid/{gridType}/ matches the last argument of @Model.Grid(...) and that the JSON is valid
  • The row renders nothing: check that Template matches a file in RowTemplates/; rows whose definition no longer exists render no columns
  • A setting has no effect: the feature must be enabled on the row definition and your template must use the property; the value is null for rows created before the feature was enabled
  • Changing a definition does not affect existing rows: default values only apply to new rows, and changing Id disconnects existing rows from the definition
To top