Skip to content

Instantly share code, notes, and snippets.

@gogromat
Last active December 14, 2015 18:09
Show Gist options
  • Save gogromat/5126933 to your computer and use it in GitHub Desktop.
Save gogromat/5126933 to your computer and use it in GitHub Desktop.
Registering Handlebars Helper (similar to Meteor's ">" helper for inner templates, except w/o the data)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8 />
<title>Testing Handlebars template</title>
</head>
<body>
<div id="test-handlebars">
{{#with author}}
{{firstName}} {{lastName}}
{{/with}}
<img src="{{gravatarurl email }}" width="96" height="96">
{{innerTemplate "inner"}}
</div>
<template name="inner">
Inner template text here :p
</template>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<!--Handlebars-->
<script type="text/javascript"
src="handlebars.js" ></script>
<!--https://tbwebframework.googlecode.com/svn-history/r248/trunk/WebsiteFramework_src/WebSite/JScript/WebToolKit/WebToolKit.MD5.js-->
<script src="WebToolKit.MD5.js"></script>
<script type="text/javascript">
$(document).ready(function() {
//Test some real-world helpers first
Handlebars.registerHelper('gravatarurl', function(email) {
return 'http://www.gravatar.com/avatar/' + MD5(email) + '.jpg?s=250';
});
// Register Handlebars helper (inner template),
// like in Meteor's ">" or in newer handlebar's ">" version
Handlebars.registerHelper("innerTemplate", function(text) {
return $("template[name="+text+"]").html();
});
// To see if Handlebars work
var test_handlebars = $("#test-handlebars").html();
var first_template = Handlebars.compile(test_handlebars);
$("#test-handlebars").html(
first_template(
{author: {firstName:"Alexander", lastName:"Astafurov"},
email:"gogromat@gmail.com"})
);
console.log("end");
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment