Fixing the Invalid Template Error When Adding an Icon to the Grid Toolbar
Environment
| Product | Progress® Kendo UI® Grid for jQuery |
| Version | 2026.4.1111 |
Description
I use a custom toolbar template in the Grid. To show an SVG icon in a toolbar button, I added a <script> element to the template that calls kendo.ui.icon with a jQuery ID selector. After this change, the Grid does not render and the browser console shows an error.
Error Message
Uncaught Error: Invalid template:'...'
Cause
When you compile the toolbar template with kendo.template or set the toolbar option as a string, Kendo UI treats the # character as the start or the end of a template expression. The # in an ID selector, such as $('#exportButton'), is not a template expression, so the template cannot be compiled.
The following template causes the error:
<script id="toolbarTemplate" type="text/x-kendo-template">
<button type="button" id="exportButton"><span class="export-icon"></span> Export</button>
<script>kendo.ui.icon($('#exportButton .export-icon'), 'file-excel');</script>
</script>
This template also nests a </script> closing tag inside an external script template. The browser ends the outer <script> element at the first closing tag, so the template content is cut off even if the hash character is escaped.
Solution
Render the icon markup directly in the template instead of initializing it with a script:
- Remove the
<script>element from the toolbar template. - Output the icon HTML with the
#= #expression and thekendo.ui.iconmethod. - Attach event handlers to the toolbar elements after the Grid is initialized.
<div id="grid"></div>
<script id="toolbarTemplate" type="text/x-kendo-template">
<button type="button" id="exportButton" class="k-button k-button-md k-rounded-md k-button-solid k-button-solid-base">
#= kendo.ui.icon({ icon: "file-excel", iconClass: "k-button-icon" }) #
<span class="k-button-text">Export</span>
</button>
</script>
<script>
$("#grid").kendoGrid({
toolbar: [
{ template: kendo.template($("#toolbarTemplate").html()) }
],
columns: [
{ field: "name" },
{ field: "age" }
],
dataSource: [
{ name: "Jane Doe", age: 30 },
{ name: "John Doe", age: 33 }
]
});
$("#exportButton").on("click", function () {
/* The result can be observed in the DevTools(F12) console of the browser. */
console.log("Export is clicked!");
});
</script>
If the template must contain a literal # character, escape it. The escape sequence differs for external script templates and for templates that are defined as JavaScript strings. For more information, refer to the article on using hash literals in templates.