Template in Blazor Toast Component

27 Sep 20214 minutes to read

Configure templates to display within a header, content, and footer section of Toast. The header, content and footer templates can be defined as HTML element inside ToastTemplates.

The following code explains how to initialize a Toast with Template.

@using Syncfusion.Blazor.Buttons
@using Syncfusion.Blazor.Notifications

<SfButton @onclick="@ShowToast"> Show Toast</SfButton>

<SfToast ID="toast_custom" @ref="@ToastObj" Width="400px" ExtendedTimeout=0 Timeout=120000>
    <ToastPosition X="Right" Y="Bottom"></ToastPosition>
    <ToastTemplates>
        <Template>
            <div>
              Toast
            </div>
        </Template>
    </ToastTemplates>
</SfToast>

@code {
    SfToast ToastObj;

    private async Task ShowToast()
    {
       await this.ToastObj.ShowAsync();
    }
}

<style>
    @@font-face {
        font-family: 'Toast_icons';
        src: url(data:application/x-font-ttf;charset=utf-8;base64,AAEAAAAKAIAAAwAgT1MvMj0gSRkAAAEoAAAAVmNtYXDnM+eRAAABsAAAAEpnbHlmzVnmlwAAAhgAAAZAaGVhZBEYIl8AAADQAAAANmhoZWEHlgN3AAAArAAAACRobXR4LvgAAAAAAYAAAAAwbG9jYQnUCGIAAAH8AAAAGm1heHABHQBcAAABCAAAACBuYW1lfUUTYwAACFgAAAKpcG9zdAxfTDgAAAsEAAAAggABAAADUv9qAFoEAAAAAAAD6AABAAAAAAAAAAAAAAAAAAAADAABAAAAAQAACcU5MF8PPPUACwPoAAAAANcI7skAAAAA1wjuyQAAAAAD6APoAAAACAACAAAAAAAAAAEAAAAMAFAABwAAAAAAAgAAAAoACgAAAP8AAAAAAAAAAQPqAZAABQAAAnoCvAAAAIwCegK8AAAB4AAxAQIAAAIABQMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUGZFZABA5wDnCgNS/2oAWgPoAJYAAAABAAAAAAAABAAAAAPoAAAD6AAAA+gAAAPoAAAD6AAAA+gAAAPoAAAD6AAAA+gAAAPoAAAD6AAAAAAAAgAAAAMAAAAUAAMAAQAAABQABAA2AAAABAAEAAEAAOcK//8AAOcA//8AAAABAAQAAAABAA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XYXJuaW5nBEluZm8HTWVldGluZwVCbGluawdTdHJldGNoA1NpcANTaXQFVHJhc2gAAAAA) format('truetype');
        font-weight: normal;
        font-style: normal;
    }

    .toast-icons {
        font-family: 'Toast_icons' !important;
        speak: none;
        font-size: 55px;
        font-style: normal;
        font-weight: normal;
        font-variant: normal;
        text-transform: none;
        line-height: 1;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }

    #toast_custom .e-toast .e-toast-title,
    #toast_custom .e-toast .e-toast-message,
    #toast_custom .e-toast .e-toast-message .e-toast-content,
    #toast_custom .e-toast .e-toast-close-icon {
        color: #fff;
    }

    #toast_custom .e-toast-container .e-toast .e-toast-message .e-toast-content {
        padding: 14px 0 0 0;
    }

    #toast_custom .e-toast-template {
        display: inline-flex;
    }

    #toast_custom .e-toast-icon.e-toast-image {
        border-radius: 50%;
        background-repeat: no-repeat;
        background-size: cover;
        height: 50px !important;
        width: 50px !important;
        background-size: 50px 50px;
        align-self: center;
    }

    #toast_custom .camden .e-toast-icon.e-toast-image,
    #toast_custom .chase .e-toast-icon.e-toast-image {
        width: 65px !important;
    }

    #template_toast .horizontal-align .toast-content .toast-title {
        font-weight: 500;
        color: #fff;
    }

    #template_toast .horizontal-align .toast-content .toast-message {
        opacity: 0.75;
        color: #fff;
    }

    #template_toast .toast-icons {
        font-size: 35px;
        height: auto;
        margin: auto;
    }

    #template_toast .horizontal-align {
        display: inline-flex;
        flex-direction: row;
        width: 100%;
    }

    #template_toast .horizontal-align .toast-content {
        display: inline-flex;
        flex: 1;
        flex-direction: column;
        margin-left: 10px;
    }
</style>

The Template can be given as a RenderFragment type for the ContentTemplate property when updating the toast templates dynamically. The below code block explains the string template.

@using Syncfusion.Blazor.Buttons
@using Syncfusion.Blazor.Notifications

<SfButton @onclick="@ShowToast"> Show Toast </SfButton>

<SfToast @ref="ToastObj" />

@code {
    SfToast ToastObj;

    private RenderFragment template = @<div id='toastEmail_template'><div class='e-toast-template'><img class='e-toast-icon e-toast-image' src='https://blazor.syncfusion.com/demos/images/toast/laura.png' /><div class='e-toast-message'><div class='e-toast-title'>Anjolie Stokes</div><div class='e-toast-content'>Networking Referral</div></div></div></div>;

    private async Task ShowToast()
    {
       await this.ToastObj.ShowAsync(new ToastModel { ContentTemplate = template });
    }
}