Skip to content

Instantly share code, notes, and snippets.

@ducin
Last active December 15, 2015 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ducin/5276720 to your computer and use it in GitHub Desktop.
Save ducin/5276720 to your computer and use it in GitHub Desktop.
ICanHaz.js `grabTemplates()` demo (see more at http://symfony-world.blogspot.com/2013/03/icanhazjs-grabtemplates-example.html). Example shows how you can load additional templates using ajax and reload ICanHaz container to use them. Note that the ajax call should be synchronous.
$(document).ready( function() {
$.ajax({
type: "GET",
dataType: 'text',
async: false,
url: "templates.html"
}).done(function(response) {
$("body").append(response);
ich.grabTemplates();
});
$('#display').click(function () {
var message = ich.displayMessage({'name': 'tkoomzaaskz'});
$('#container').append(message);
});
});
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/tr/html4/transitional.dtd">
<html>
<head>
<title>ICanHaz.js grabTemplates demo</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/ICanHaz.js/0.10/ICanHaz.min.js"></script>
<script type="text/javascript" src="ajax-ich.js"></script>
</head>
<body>
<p>
Once the document is ready, templates are loaded via ajax and
appended to the end of the body tag. <code>ich.grabTemplates()</code>
loads them into ICanHaz.
</p>
<button id="display">click to add message</button>
<div id="container"></div>
</body>
</html>
<script id="displayMessage" type="text/html">
<p>
Hello {{ name }} ajax call succeeded!
</p>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment