Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extends functionality of the Haml adapter. Add simple partials support. Now w... #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 31 additions & 7 deletions lib/plugins/sammy.haml.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,46 @@
//
Sammy.Haml = function(app, method_alias) {
var haml_cache = {};
// Helper for run Haml render
// ### Arguments
// * `name` internal cache key, String
// * `template` haml text, String
// * `data` An Object containing the replacement values for the template
//
var _execute = function(name, template, data) {
var fn = haml_cache[name];
if (!fn) {
fn = haml_cache[name] = Haml(template);
}
return fn(data);
}
// *Helper* Uses haml-js to parse a template and interpolate and work with the passed data
//
// ### Arguments
//
// * `template` A String template.
// * `data` An Object containing the replacement values for the template.
// data is extended with the <tt>EventContext</tt> allowing you to call its methods within the template.
//
var haml = function(template, data, name) {
// * `partials` An Object containing partials String templates. Partials use the same data as template use.
// Keys of Object is used to access to partial in main template, example:
// partials argument: {link: 'a.menu{href: 'http://example.com'} = var_link_title', footer: 'Copyright asnow.dev'}
// haml: = render_partial_link
// = render_partial_footer
//
var haml = function(template, data, partials, name) {
// use name for caching
if (typeof partials != 'object') { name = partials; }
if (typeof name == 'undefined') { name = template; }
var fn = haml_cache[name];
if (!fn) {
fn = haml_cache[name] = Haml(template);
// prepare data
var merged_data = $.extend({}, this, data);
// render partials
if (typeof partials == 'object') {
for (var partial_name in partials ) {
var partial_cache_key = '_' + name + '_' + partial_name;
merged_data['render_partial_' + partial_name] = _execute(partial_cache_key, partials[partial_name], merged_data);
}
}
return fn($.extend({}, this, data));
return _execute(name, template, merged_data);
};

// set the default method name/extension
Expand All @@ -76,4 +100,4 @@

};

})(jQuery);
})(jQuery);