From abf4db212db35292cbfe2477d552192da44c6dd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=BE=D0=BB=D1=8C=D1=88=D0=BE=D0=B2=20=D0=90=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=B5=D0=B9?= Date: Thu, 23 Feb 2012 01:07:20 +0300 Subject: [PATCH] Extends functionality of the adapter. Add simple partials support. Now we can use partails in template by variable "render_partial_[partial_key]". But still can't use partial in partials. Data sended to template is available in partials, but it shared --- lib/plugins/sammy.haml.js | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/plugins/sammy.haml.js b/lib/plugins/sammy.haml.js index 87039f90..15598ba5 100644 --- a/lib/plugins/sammy.haml.js +++ b/lib/plugins/sammy.haml.js @@ -52,6 +52,19 @@ // 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 @@ -59,15 +72,26 @@ // * `template` A String template. // * `data` An Object containing the replacement values for the template. // data is extended with the EventContext 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 @@ -76,4 +100,4 @@ }; -})(jQuery); +})(jQuery); \ No newline at end of file