Tuesday, 5 February 2013

jQuery Template

pass a string:
$.tmpl("<div class="myClass">${FirstName},/div>", myObject).appendTo("#myDiv");

use a selector to find a script block on the page:
$("#myTemplate").tmpl(myObjects).appendTo("#myDiv");

...

<script id="myTemplate" type="text/x-jquery-tmpl">
    <tr>
        <td>${FirstName},/td>
        <td>${LastName},/td>
        <td>${Twitter},/td>
    </tr>
</script>

get an external "html" which contain the html template:

$.get("MyTemplate.htm", function (data) {
    $.tmpl(data, myObjects).appendTo("#myDiv");
});

No comments:

Post a Comment