Template in EJ2 TypeScript Toast control

10 May 20234 minutes to read

The Template property in toast can be defined as HTML element, this can be either a string or selector.

The HTML element tag can be given as a string for the Template property.

template: "<div>Toast Content</div>"

The Template property allows getting the template content using the query selector. Here, the element ‘ID’ attribute is specified in the template.

template: "#Template"
import { Toast, ToastOpenArgs, ToastCloseArgs, ToastBeforeOpenArgs } from '@syncfusion/ej2-notifications';
import { DropDownList, ChangeEventArgs } from '@syncfusion/ej2-dropdowns';
import { compile, Browser, closest } from '@syncfusion/ej2-base';

let toast: Toast = new Toast({
  template: document.getElementById('template_toast_ele').innerHTML,
  position:  { X: 'Right', Y: 'Bottom' },
  target: document.body,
  extendedTimeout: 0,
  timeOut: 120000
});
toast.appendTo('#element');
toast.show();

let btnEle: HTMLElement = document.getElementById('toast_mail_remainder');
let snoozeFlag: boolean = false;

document.getElementById('show_toast').onclick = () => {
    toast.show();
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 Toast</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript Toolbar Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="index.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/material.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    <div id='loader'>LOADING....</div>
    <div id='container'>
      <div class='row'> <button class="e-btn" id="show_toast"> Show Toast</button> </div>
      <div id="template_toast"> <div id='element'></div> </div>
        
        <br/><br/>
        <div id='result'></div>
    </div>
    <script id="template_toast_ele" type="text/x-template">
    <div id='template_toast' style="display: none">
        <div class="horizontal-align">
            <div class='toast-content'>
                <div class='toast-title'>
                    Weekend Alarm
                </div>
                <div class='toast-message'> With traffic, its likely to take 45 minutes to get to jenny's 24th Birthday Bash at Hillside Bar, 454 E.
                    Olive Way by 10:00PM </div>
            </div>
        </div>
        <img src="https://ej2.syncfusion.com/demos/src/toast/resource/map.jpg" width="100%" height="70%">
    </div>
</script>
</body>

</html>

See Also