lib/fanforce/utils.rb in fanforce-0.3.6 vs lib/fanforce/utils.rb in fanforce-0.3.7
- old
+ new
@@ -118,6 +118,29 @@
def decode_json(str, symbolize_keys=true)
MultiJson.load(str, :symbolize_keys => symbolize_keys)
end
+ # Creates a string representation of a javascript object for $.tmpl
+ def compile_jquery_tmpls(options={}, &block)
+ begin require 'haml' rescue LoadError raise "You must have the haml gem installed for Fanforce.compile_jquery_templates to work." end
+ context = Object.new
+ class << context
+ include Haml::Helpers
+ end
+ context.init_haml_helpers
+
+ format = if options[:format] == 'html' then :html else options[:callback].present? ? :jsonp : :json end
+
+ return context.capture_haml(&block) if format == :html
+ single_line_html = context.capture_haml(&block).split(/\r?\n/).inject('') {|sl, l| sl += l.strip + ' ' }
+ matches = single_line_html.scan(/<script id=[\"'](.*?)[\"'](?:.*?)>(.*?)(?:<\/script>)/mi)
+
+ templates = matches.inject({}) {|t,m| t[m[0]] = m[1]; t }
+ if format == :jsonp
+ "#{options[:callback]}(#{templates.to_json})"
+ else
+ templates.to_json
+ end
+ end
+
end