The content tag helper renders a content property value inside an HTML element. The tag helper adds some convenience attributes that are not available when rendering the property value directly. The tag helper can also be used with other (non-Redakt) property values.

Usage

The content tag helper can be applied to any HTML element by adding the rx-content attribute. The property value will then be rendered inside the element. The following attributes can be used:

  • rx-content required - the property value to render. This should be the actual property value, not the name of the property. For non-string values, the output will be equivalent to calling .ToString() on the value.
  • rx-default optional - the default value for the property which will be rendered if the property is empty.
  • rx-if-empty - the action to take if both the property and the default values are empty. The following options are available:
    • EmptyAction.None default - do nothing; any pre-existing element content will still be rendered.
    • EmptyAction.ClearContent - clears any content that may already exist inside the element.
    • EmptyAction.RemoveTag - completely removes the element from the output.
  • rx-if-exists - the action to take if the element already has content. The following options are available:
    • ContentAction.Replace default - replaces any pre-existing element content with the property value.
    • ContentAction.Prepend - prepends the property value to any pre-existing element content.
    • ContentAction.Append - appends the property value to any pre-existing element content.
    • ContentAction.LeaveExisting - leaves the existing content in place, i.e. does not render the property value.

Examples

<body>
    <span rx-content="@Model.Content.Title" rx-default="@Model.Content.NavTitle">Fallback Title</span>

    <p rx-content="@Model.Content.IntroText" rx-if-empty="EmptyAction.RemoveTag"><p>
    
    <p rx-content="@Model.Content.IntroText" rx-if-empty="EmptyAction.ClearContent" rx-if-exists="ContentAction.Append">This is the intro:<br/><p>

    <label rx-content="@Model.Content.LabelText" rx-if-exists="ContentAction.Prepend"> label</span>

    <div rx-content="@Model.Content.FallbackValue" rx-if-exists="ContentAction.LeaveExisting">@SomePriorityValue</div>

    <p rx-content="@Model.Content.IntroText" rx-if-exists="ContentAction.LeaveExisting">@SomePriorityValue</div>
</body>