Skip to content

Instantly share code, notes, and snippets.

@jnf
Created February 15, 2013 18:50
Show Gist options
  • Save jnf/4962454 to your computer and use it in GitHub Desktop.
Save jnf/4962454 to your computer and use it in GitHub Desktop.
//generic tag helpers
Handlebars.registerHelper('generic_tag', function(tag, text, options) {
var attrs = [];
for(var prop in options.hash) { attrs.push(prop + '="' + options.hash[prop] + '"'); }
return new Handlebars.SafeString( "<" + tag + " " + attrs.join(" ") + ">" + text + "</" + tag + ">" );
});
Handlebars.registerHelper('generic_selfclosing_tag', function(tag, options) {
var attrs = [];
for(var prop in options.hash) { attrs.push(prop + '="' + options.hash[prop] + '"'); }
return new Handlebars.SafeString( "<" + tag + " " + attrs.join(" ") + " />" );
});
//shortcuts for common tags
Handlebars.registerHelper('link_to', function(text, options) {
var attrs = [];
for(var prop in options.hash) { attrs.push(prop + '="' + options.hash[prop] + '"'); }
return new Handlebars.SafeString( "<a " + attrs.join(" ") + ">" + text + "</a>" );
});
Handlebars.registerHelper('img_tag', function(options) {
var attrs = [];
for(var prop in options.hash) { attrs.push(prop + '="' + options.hash[prop] + '"'); }
return new Handlebars.SafeString( "<img " + attrs.join(" ") + " />" );
});
//simple wrapper for swf_object
Handlebars.registerHelper('swf_object_helper', function(options) {
var attrs = [], param_order = [
'path', 'id', 'width', 'height', 'version', 'install_path', 'flash_vars', 'params', 'attributes', 'callbacks'
],
defaults = {
path: "",
id: "",
width: "100%",
height: "100%",
version: "9.0.0",
install_path: "/express_install.swf",
flash_vars: "{}",
attributes: "{}",
callbacks: null
};
for (var i = param_order.length - 1; i >= 0; i--) {
var param = param_order[i];
attrs.push("'" + options[param].length > 0 ? options[param] : defaults[param] + "'");
};
return new Handlebars.SafeString("<script type='text/javascript'>swfobject.embedSWF(" + attrs.join(", ") + ")</scipt>");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment