Table of Contents

Item types in templates

Define an item type and build the paragraph templates and page layouts that render it

An item type is a content schema you define for a solution: a named set of fields that editors fill out and that your templates render. If you have worked with a headless CMS, think of it as a content type or a block definition. A Teaser item type could have a Heading, an Image, a Text, and a Link; editors create teasers, and your Teaser.cshtml template decides what the markup looks like.

This article explains how to define an item type, where DynamicWeb looks for the templates that render it, and how to read item fields in Razor. The settings UI for item types is covered field by field in the Manual under Item types.

Where item types are used

An item type is always attached to something else in the content structure. When you define it, you decide what it can be attached to, and that decides which ViewModel your template reads it from.

The table below lists the places an item type can be used and how to reach it from a template:

Used as What editors get Read it in a template from
Paragraph type A new kind of content block they can add to a page Model.Item in a ParagraphViewModel template
Page type A page with a fixed set of fields, e.g. a blog post Model.Item in a PageViewModel layout
Row settings Extra settings on a grid row, e.g. a background color Model.Item in a GridRowViewModel row template
Website settings Site-wide settings, e.g. logo, header, or footer Model.Area.Item in any PageViewModel layout
Page properties Extra settings on every page, e.g. a hero image Model.PropertyItem in a PageViewModel layout

Paragraph types and page types are the ones you build templates for. Row, website, and page property items are settings you read inside templates you already have.

Defining an item type

Item types are created in the administration under Settings > Areas > Content > Item types. For a paragraph type, the minimum steps are:

  1. Click New item type, give it a name and a system name, e.g. Teaser
  2. Add the fields, each with a name, a system name, and a field type, e.g. Heading (text), Image (file), Text (rich text), and Link (link)
  3. Open Item type restrictions and set what the item type is enabled for, e.g. Paragraphs, and which websites it is allowed in
  4. Save

New item types are not available anywhere until you set the restrictions in step 3. If editors cannot find your new paragraph type, check the restrictions first.

The system names are the contract between the content and your templates. Templates look up fields by system name, and DynamicWeb finds templates by the item type system name. Pick them carefully and do not rename them once content exists.

Where the definition is stored

Each item type is saved as an XML file in /Files/System/Items/, named ItemType_{SystemName}.xml. DynamicWeb creates a database table with the same name, ItemType_{SystemName}, to hold the field values of each item.

This means the definition is a file you can keep in source control and deploy together with your design. The content, the items editors create, lives in the database and is not part of the file.

Below is a trimmed example of a definition. You rarely write this by hand, but it is useful to recognize what the administration produces:

<items>
  <item name="Teaser" systemName="Teaser" category="MyDesign/Paragraphs" fieldForTitle="Heading">
    <fields>
      <field name="Heading" systemName="Heading" type="System.String, System.Private.CoreLib">
        <editor type="Dynamicweb.Content.Items.Editors.TextEditor, Dynamicweb" />
      </field>
      <field name="Image" systemName="Image" type="System.String, System.Private.CoreLib">
        <editor type="Dynamicweb.Content.Items.Editors.FileEditor, Dynamicweb" />
      </field>
      <!-- Text and Link fields omitted -->
    </fields>
    <rules>
      <rule name="Allow in websites" type="Dynamicweb.Content.Items.Activation.AreaRestrictionRule, Dynamicweb" value="*" />
      <rule name="Enable item type for" type="Dynamicweb.Content.Items.Activation.StructureRestrictionRule, Dynamicweb" value="Paragraphs" />
    </rules>
  </item>
</items>

Swift ships with more than 100 item types in this format. If you have a Swift solution available, /Files/System/Items/ is a good place to see how real item types are put together.

Note

Item types can also be defined in C# code by inheriting from Dynamicweb.Content.Items.ItemEntry and decorating the class and its properties with the attributes in Dynamicweb.Content.Items.Annotations. That approach is useful when item types belong to an add-in rather than to a design.

Paragraph templates for an item type

When a paragraph is based on an item type, DynamicWeb looks for a template named after the item type system name in the design's Paragraph folder. For the Teaser item type in the design MyDesign, that is:

/Files/Templates/Designs/MyDesign/
├── Paragraph/
│   ├── Default.cshtml
│   ├── Teaser.cshtml

A paragraph template inherits the ParagraphViewModel, and the item fields are available on Model.Item:

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

<article class="teaser">
    @if (Model.Item.TryGetImageFile("Image", out ImageFileViewModel image))
    {
        <img src="@image.ToGetImage(new() { Width = 600, Ratio = "4/3" })" alt="@image.Name" loading="lazy">
    }

    <h2>@Model.Item.GetString("Heading")</h2>

    @if (Model.Item.TryGetString("Text", out string text))
    {
        <div class="teaser-text">@text</div>
    }

    @if (Model.Item.TryGetLink("Link", out LinkViewModel link))
    {
        <a href="@link.Url" class="btn btn-primary">Read more</a>
    }
</article>

Keep a Default.cshtml (or at least one template) directly in the Paragraph folder. DynamicWeb uses the location of the design's default paragraph template to find item type templates, and the template selector in the paragraph editor relies on it too.

Several layouts for one item type

Often editors need the same content in different visual variants, e.g. a teaser with the image on the left or on top. Instead of one template, create a subfolder named after the item type system name and put the variants in it:

/Files/Templates/Designs/MyDesign/
├── Paragraph/
│   ├── Default.cshtml
│   ├── Teaser/
│   │   ├── ImageTop.cshtml
│   │   ├── ImageLeft.cshtml
│   │   ├── LayoutSettings.config

Editors then pick one of the variants on the paragraph. If they have not picked one, DynamicWeb uses the first template the file system returns from the folder. To make the default predictable, add a LayoutSettings.config file to the subfolder. It is a JSON file with a default template and a list of templates to hide from editors:

{
  "DefaultTemplate": "ImageTop.cshtml",
  "ExcludedTemplates": [ "_TeaserHelpers.cshtml" ]
}

This is the pattern Swift uses. For example, the Swift Swift-v2_Button paragraph type has the templates ButtonLeft.cshtml, ButtonCenter.cshtml, and ButtonRight.cshtml in Paragraph/Swift-v2_Button/.

How a paragraph template is chosen

For an item-based paragraph, DynamicWeb picks the first match in this order:

  1. The template set on the paragraph, looked up in Paragraph/{ItemType}/ first and then in Paragraph/
  2. A template mapped to the item type in the website settings
  3. Paragraph/{ItemType}.cshtml
  4. A template in the Paragraph/{ItemType}/ subfolder, using LayoutSettings.config or the first file
  5. The default paragraph template of the design, i.e. Paragraph/Default.cshtml or the first template in the folder

Step 1 does not check that the file exists. If a template name is stored on the paragraph, DynamicWeb uses it even after the file has been renamed or deleted, and does not continue to the next steps.

The last step is the reason a missing item type template does not throw an error. The paragraph is rendered with the default template instead, which usually means you see the wrong markup or no item fields at all. Check the file name against the item type system name when that happens.

Page layouts for an item type

A page can be based on an item type as well. This is the page-based items content model: the page fields are the content, and the layout decides how they are presented. A blog post or a news article are typical examples.

To create a layout for a page item type, name it after the item type system name and place it in the root of the design next to your other layouts:

/Files/Templates/Designs/MyDesign/
├── Master.cshtml
├── Layout_default.cshtml
├── BlogPost.cshtml

The layout inherits the PageViewModel. The page's item fields are on Model.Item:

@inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.PageViewModel>
@using Dynamicweb.Frontend
@MasterPageFile("Master.cshtml")

<article class="container">
    <h1>@Model.Item.GetString("Title")</h1>
    <p class="lead">By @Model.Item.GetString("Author"), @Model.Item.GetDateTime("Date").ToString("d")</p>

    @if (Model.Item.TryGetImageFile("Image", out ImageFileViewModel image))
    {
        <img src="@image.ToGetImage(new() { Width = 1200 })" alt="@image.Name" class="img-fluid">
    }

    @Model.Item.GetString("Text")

    @* Optional: let editors add extra paragraphs below the fixed content *@
    @Model.Placeholder("content", "Additional content")
</article>

A page item layout can still contain placeholders or a grid if you want editors to add free content in addition to the fixed fields.

How a page layout is chosen

For a page, DynamicWeb picks the first match in this order:

  1. The layout selected on the page itself
  2. A layout selected on a parent page with Inherit to subpages enabled
  3. For item-based pages, a layout mapped to the item type in the website settings
  4. For item-based pages, {ItemType}.cshtml in the same folder as the website's default layout
  5. The website's default layout

Because the item type layout is only used when nothing more specific is set, an editor who explicitly picks another layout on a blog post page overrides your BlogPost.cshtml.

Reading item fields

Model.Item is an ItemViewModel. It gives you typed access to each field by system name. The lookup is case-insensitive, and a field that does not exist returns null or a default value rather than throwing.

Most accessors come in two forms:

  • GetX("Field") returns the value, or null or a default when the field is empty
  • TryGetX("Field", out var value) returns true only when the field has a value, which fits well with @if blocks so you do not render empty wrappers

The table below maps common field types to the accessor to use:

Field type Accessor Returns
Text, long text, rich text GetString / TryGetString string
Checkbox GetBoolean bool
Integer, decimal GetInt32, GetDecimal, GetDouble Number
Date, date and time GetDateTime / TryGetDateTime DateTime
File (image) TryGetImageFile, TryGetImageFiles ImageFileViewModel
File (other), multiple files GetFile, GetFiles FileViewModel
Link GetLink / TryGetLink LinkViewModel with Url
Button GetButton / TryGetButton ButtonViewModel with Label, Style, and Link
Dropdown, radio, checkbox list GetList / TryGetList, or GetString ListViewModel with the selected options
Item link, item relation list GetItem, GetItems Nested ItemViewModels
Color GetColor / TryGetColor ColorViewModel
User, user group GetUser, GetUsers, GetUserGroup User and group ViewModels

GetRawValueString returns the value exactly as stored, which can help when you debug what a field contains. Model.Item.Fields lists all fields, so you can loop over them while you build a template to see names and values.

Images

Image fields return an ImageFileViewModel. Use its ToGetImage() extension to get a resized, cropped, and converted image URL instead of linking to the original file. The Image handler article explains the settings and how to build responsive srcset markup.

Lists of items

An item relation list field holds a list of child items, each with its own item type. Use it for repeating content inside one paragraph, e.g. the slides in a slider or the questions in an FAQ:

<div class="accordion">
    @foreach (var question in Model.Item.GetItems("Questions"))
    {
        <details>
            <summary>@question.GetString("Question")</summary>
            @question.GetString("Answer")
        </details>
    }
</div>

Each child is an ItemViewModel, so you read its fields the same way as on the parent.

Output is not HTML-encoded

DynamicWeb's Razor engine writes values to the output as-is. Unlike ASP.NET Core MVC views, @value does not HTML-encode the string. This is what makes rich text fields render as HTML without @Html.Raw(), but it also means plain text fields are not escaped for you.

When you write editor or user input into an attribute or into a context where markup is not expected, encode it yourself, e.g. with System.Net.WebUtility.HtmlEncode().

Settings items in existing templates

Row, website, and page property item types do not get templates of their own. You read them from the templates you already have:

  • Row settings: set the ItemType property on a row definition in the grid and read Model.Item in the row template
  • Website settings: select the item type in the website settings and read Model.Area.Item in your layouts, e.g. for a logo or a footer text shared by all pages
  • Page properties: select the item type for page properties in the website settings and read Model.PropertyItem in your layouts, e.g. for a hero image or a meta setting on each page

A website settings item, used from the master template, could look like this:

@if (Model.Area.Item?.TryGetImageFile("Logo", out ImageFileViewModel logo) == true)
{
    <a href="/" class="navbar-brand">
        <img src="@logo.ToGetImage(new() { Height = 40 })" alt="@Model.Area.Name">
    </a>
}

Use ?. when you read these items. A website or page without the item type attached has Item or PropertyItem set to null.

Summary

To add an item-based paragraph type to a design:

  1. Create the item type, add fields, and enable it for paragraphs under Item types
  2. Add Paragraph/{SystemName}.cshtml, or a Paragraph/{SystemName}/ folder with one template per variant
  3. Inherit the ParagraphViewModel and read the fields from Model.Item by system name
  4. Add a paragraph of the new type to a page and preview it

For a page item type, the steps are the same, except that the template is a layout named {SystemName}.cshtml in the root of the design and inherits the PageViewModel.

To top