Templated Views & Slots

Build your own Tag Helpers whose markup is a Razor view, and pass content into named slots

In ASP.NET Core, a Tag Helper builds its HTML in C#. StellarAdmin.UI lets you write the markup of your own Tag Helpers in Razor instead:

  • A templated Tag Helper renders a Razor view, which you write like a partial view.
  • A named slot lets the page that uses a Tag Helper pass content to a specific place in the Tag Helper's markup.

Creating a templated Tag Helper

To create a templated Tag Helper, derive your Tag Helper from StellarAdminTemplatedTagHelperBase. Return the name of the view in the ViewName property, and the model of the view from the GetViewModel() method.

The example below defines a <docs-trip-card> Tag Helper that renders the _TripCard view, with the booking as the model:

[HtmlTargetElement("docs-trip-card")]
public class TripCardTagHelper : StellarAdminTemplatedTagHelperBase
{
    [HtmlAttributeName("booking")]
    public Booking? Booking { get; set; }

    protected override string ViewName => "_TripCard";

    public TripCardTagHelper(ICompositeViewEngine viewEngine)
        : base(viewEngine) { }

    protected override object? GetViewModel()
    {
        return Booking;
    }
}

The Tag Helper finds the view the same way as a partial view. The example below defines the view in Pages/Shared/_TripCard.cshtml:

Pages/Shared/_TripCard.cshtml
@model DocsSamples.Booking

<sa-card class="w-full max-w-sm">
    <sa-card-header>
        <sa-card-title>@Model.Destination</sa-card-title>
        <sa-card-description>Booking @Model.Id</sa-card-description>
    </sa-card-header>
    <sa-card-content>
        <div class="flex items-center justify-between text-sm">
            <sa-badge variant="BadgeVariant.Outline">@Model.Status</sa-badge>
            <span class="font-medium">@Model.Amount.ToString("C")</span>
        </div>
    </sa-card-content>
</sa-card>

The page that uses the Tag Helper only needs the <docs-trip-card> element:

<docs-trip-card booking="StaticData.Bookings[0]"/>

The Tag Helper doesn't render an element of its own, only the view. It also doesn't render its child content, except for slot content.

Rendering a different view

To render a different view for one use of a templated Tag Helper, specify the view's name in the view attribute. You can also specify a path relative to the root of your app.

The example below renders the _TripCardCompact view:

<docs-trip-card booking="StaticData.Bookings[1]" view="_TripCardCompact"/>

Passing content to a slot

To let the page that uses your templated Tag Helper supply part of its markup, add a <sa-slot-outlet> to the view, with the slot's name. The content of <sa-slot-outlet> is the fallback content, which is rendered when the page doesn't supply content for the slot.

The example below is the footer of the _BookingCard view of a <docs-booking-card> Tag Helper. It adds an actions slot, with a View details button as the fallback content:

Pages/Shared/_BookingCard.cshtml
<sa-card-footer>
    <sa-slot-outlet name="actions">
        <sa-button size="ButtonSize.Small" variant="ButtonVariant.Outline">View details</sa-button>
    </sa-slot-outlet>
</sa-card-footer>

To supply the content for the slot, add a <sa-slot-content> with the slot's name inside the Tag Helper. The example below replaces the View details button with Check in and Cancel trip buttons:

<docs-booking-card booking="StaticData.Bookings[2]">
    <sa-slot-content name="actions">
        <sa-group gap="GroupGap.Small">
            <sa-button size="ButtonSize.Small">Check in</sa-button>
            <sa-button size="ButtonSize.Small" variant="ButtonVariant.Outline">Cancel trip</sa-button>
        </sa-group>
    </sa-slot-content>
</docs-booking-card>

When the page doesn't supply content for the slot, the view renders the fallback content:

<docs-booking-card booking="StaticData.Bookings[3]"/>

Using slots in a Tag Helper that builds its HTML in C#

A Tag Helper that derives from StellarAdminTagHelperBase can also receive slot content, and render it where you want in C#. Call GetChildContentAsync() first, which runs the <sa-slot-content> elements in the child content, and then get the content of a slot with TryGetNamedSlot(). The <sa-slot-content> elements don't render anything in the child content.

The example below defines a <docs-booking-summary> Tag Helper that renders the actions slot in its header, and the rest of the child content in its body:

[HtmlTargetElement("docs-booking-summary")]
public class BookingSummaryTagHelper : StellarAdminTagHelperBase
{
    [HtmlAttributeName("booking")]
    public Booking? Booking { get; set; }

    public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
    {
        var childContent = await output.GetChildContentAsync();

        output.TagName = "div";
        output.Attributes.SetAttribute("class", "booking-summary");

        output.Content.AppendHtml("<div class=\"booking-summary-header\">");
        output.Content.Append(Booking?.Destination ?? string.Empty);

        if (TryGetNamedSlot("actions", out var actions))
        {
            output.Content.AppendHtml(actions);
        }

        output.Content.AppendHtml("</div>");
        output.Content.AppendHtml("<div class=\"booking-summary-body\">");
        output.Content.AppendHtml(childContent);
        output.Content.AppendHtml("</div>");
    }
}

The page that uses the Tag Helper can put the <sa-slot-content> anywhere in the child content:

<docs-booking-summary booking="StaticData.Bookings[0]">
    <sa-slot-content name="actions">
        <sa-button size="ButtonSize.ExtraSmall" variant="ButtonVariant.Outline">Edit</sa-button>
    </sa-slot-content>
    <p>Two travelers, departing 12 October. Payment received in full.</p>
</docs-booking-summary>

API Reference

<sa-slot-content>

Supplies its child content for a named slot of the closest StellarAdmin.UI Tag Helper it's in. It doesn't render anything where it is. Supplying content for the same slot twice in the same Tag Helper throws an exception.

Prop

Type

<sa-slot-outlet>

Renders the content supplied for the named slot of the templated Tag Helper that renders the view. When no content is supplied, or the view is rendered as a partial view instead of by a templated Tag Helper, it renders its own child content. It doesn't render an element of its own.

Prop

Type

On this page