I must be missing something basic here but while developing a custom jQuery widget, how do we include a html template as part of the widget like we do with dojo custom widgets? Currently, my jQuery custom widget is generating the needed html in JavaScript and I am trying to take that out into a html template and include it with the custom widget. Thoughts?
jQuery custom widgets: how can I include a html template?
3.6k Views Asked by Ramya At
2
There are 2 best solutions below
0

You don't really need a plugin or anything fancy to do what you're trying to do. Widgets can be created easily enough by simply creating html files with the appropriate markup and any jQuery calls you need. For example you can create a main page like this:
<html>
<head>
<!-- reference jquery library here -->
<script>
$(document).ready(function() {
$("#wdg1").load("mywidget.html");
$("#wdg2").load("mywidget.html");
});
</script>
</head>
<body>
<div id="wdg1"></div>
<div id="wdg2"></div>
</body>
</html>
And then your mywidget.html
file will look something like:
<script>
$(document).ready(function() {
alert("Widget loaded.");
// run whatever code you want here.
});
</script>
<span>This span was loaded as a widget.</span>
You can have a look at Handlebars.js. THis is a jQuery template plugin and The syntax is pretty easy
Declare a Templae with
script
tagThen fetch and compile that template like this
Then you can render that template by passing data for
palceholders
like thisAfter this you will have the ful
html
inhtml
variable which you can use in any jQuery DOM manipulation method like$.append
.Working Fiddle