Skip to content

Instantly share code, notes, and snippets.

@stevepeak
Last active December 15, 2015 04:09
Show Gist options
  • Save stevepeak/5199549 to your computer and use it in GitHub Desktop.
Save stevepeak/5199549 to your computer and use it in GitHub Desktop.
Generate Hogan templates on Bootstrap on the fly.
$(document).on('click', '[data-toggle][data-template]', function(){
var target = $($(this).attr('data-target') || $(this).attr('href'));
if( !$(target).data('hogan-template') ){
$(target).data('hogan-template',Hogan.compile(target.html()));}
target.html($(target).data('hogan-template').render($.parseJSON($(this).attr('data-template'))));
});
<a href="#modal" data-toggle="modal" class="btn btn-small" data-template='{"title":"Hello","body":"Some stuff.","more":"more data"}'>Open</a>
<a href="#modal" data-toggle="modal" class="btn btn-small" data-template='{"title":"Yo!","body":"Put your juicy stuff here.","more":"more..."}'>Open 2</a>
<div id="modal" class="modal hide fade">
<div class="modal-header"><h3>{{title}}</h3></div>
<div class="modal-body">
<p>{{body}}</p>
{{more}}
</div>
</div>
@stevepeak
Copy link
Author

This script is a quick and easy way to render templates on the fly.
This will on the fly generate the html content. All of Hogan template methods work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment