Sha256: 2620dba75342b296abc33f5b823ba839ba11548a7603b522dbd80903bdf27013
Contents?: true
Size: 1.89 KB
Versions: 13
Compression:
Stored size: 1.89 KB
Contents
module Fanforce::Plugin::Sinatra::RactiveHelpers # Creates a string representation of a javascript object for ractive.js def compile_ractive_templates(options={}, &block) format = options[:format] == 'html' ? :html : :json context = create_context html = context.capture_haml(&block) html.gsub!(/([^\s])\s*\{\{\/if}}(\s*\{\{elseif)/, '\1\2') # if/elseif html.gsub!(/([^\s])\s*\{\{\/if}}(\s*\{\{else)/, '\1\2') # if/else html.gsub!(/([^\s])\s*\{\{\/elseif}}(\s*\{\{elseif)/, '\1\2') # elseif/elseif html.gsub!(/([^\s])\s*\{\{\/elseif}}(\s*\{\{else)/, '\1\2') # elseif/else html.gsub!(/{\{\/elseif}}/, '{{/if}}') # /elseif -> /if html.gsub!(/{\{\/else}}/, '{{/if}}') # /else -> /if return html if format == :html single_line_html = html.split(/\r?\n/).inject('') {|sl, l| sl += l.strip + ' ' } matches = single_line_html.scan(/<script id=[\"'](.*?)[\"'](?:.*?)>(.*?)(?:<\/script>)/mi) matches.inject({}) {|t,m| t[m[0]] = m[1]; t }.to_json end def RACTIVE(command, helper, &block) begin require 'haml' rescue LoadError raise 'You must have the haml gem installed to use Fanforce.compile_jquery_templates.' end raise ArgumentError, 'Missing block' unless block_given? opener = "#{command}" opener += " #{helper}" if helper closer = command.gsub('#','') context = create_context "{{#{opener}}}\n" + context.capture_haml(&block).to_s.split("\n").inject('') {|html, l| html += " #{l}\n"} + "{{/#{closer}}}" end def IF(helper, &block) RACTIVE('#if', helper, &block) end def ELSE_IF(helper, &block) RACTIVE('elseif', helper, &block) end def ELSE(&block) RACTIVE('else', nil, &block) end def FOREACH(helper, &block) RACTIVE("##{helper}", nil, &block) end def create_context context = Object.new class << context include Haml::Helpers end context.init_haml_helpers context end end
Version data entries
13 entries across 13 versions & 1 rubygems