Client CraftClient Craft HubStart free

Point fields at elements with `data-property` and `data-property-node`

On this page
Updated 24 Nov 20262 min read

Add data-property hints to your Razor so editors can click the element a setting changes, including site settings fields.

Plan: Free Visual Builder package

The Visual Builder finds text, rich text, links and images on the page by itself. Settings such as toggles, dropdowns and colours only change classes or markup, so the builder can't know which element they belong to. A data-property attribute tells it. It is plain HTML: no tag helper, and ignored on the live site.

Before you start #

  • The page is shown in the Visual Builder.
  • You know the property aliases of your blocks and document types.

Step by step #

  1. 1
    Hint a block settingIn a block view, put data-property with the setting's alias on the element (or elements) it changes:
    razor
    1<span class="quote-mark" data-property="accentColour"></span>

    When the editor selects that element, the setting's editor appears on the block's bar. Several elements may name the same field: pointing at its control outlines all of them. One element can name several fields, separated by spaces (data-property="accentColour showIcon").

  2. 2
    Hint a field of the page in a layout or partialThe same attribute works outside blocks. Without data-property-node, the alias is a field of the page being edited:
    razor
    1<nav class="breadcrumbs" data-property="hideBreadcrumb">
    2 @* breadcrumb markup *@
    3</nav>

    The element becomes selectable, with the field's editor on its bar and a button for all page properties.

  3. 3
    Hint a field of another documentFor values that come from another document, such as a site settings node, add data-property-node with that document's key:
    razor
    1@{
    2 var siteSettings = Model.Root().FirstChild(x => x.ContentType.Alias == "siteSettings");
    3}
    4@if (siteSettings is not null)
    5{
    6 <div class="info-banner" data-property="infoText" data-property-node="@siteSettings.Key">
    7 @(siteSettings.Value<string>("infoText"))
    8 </div>
    9}

    The builder shows where the value comes from ("From Global configuration") and an Open … ↗ button that opens that document in a new tab. It never edits the other document in place.

Unknown aliases #

  • In a block view, an alias the block doesn't have is ignored. The block's Advanced tab names it: "This block's view names data-property="…", but the block has no field with that alias."
  • In a layout, an alias the page doesn't have is ignored, and the editor sees a one-time warning: "Unknown field in data-property".
TipPrefer elements that don't already show a text field. Clicking a text element keeps selecting its text, though it is still outlined when you point at the hinted field's control.

What happens next #

Put it together in Make a custom block editable in the Visual Builder.

Was this article helpful?