Sha256: 67cdde967c4aa8d2fa1c7b85c1146759076e4422178ad5925a0369b2dd0a1dc1

Contents?: true

Size: 1.29 KB

Versions: 15

Compression:

Stored size: 1.29 KB

Contents

module Fanforce::App::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)
    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(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?

    context = create_context
    helper = '#'+helper if helper !~ /^[#\^]/
    "{{#{helper}}}\n" + context.capture_haml(&block) + "{{/#{helper.gsub(/(#|\^)/,'')}}}"
  end

  def IF(helper, &block)
    RACTIVE(helper, &block)
  end

  def ELSE_IF(helper, &block)
    RACTIVE(helper, &block)
  end

  def FOREACH(helper, &block)
    RACTIVE(helper, &block)
  end

  def create_context
    context = Object.new
    class << context
      include Haml::Helpers
    end
    context.init_haml_helpers
    context
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
fanforce-app-factory-2.0.0.rc38 lib/fanforce/app_factory/sinatra/helpers/ractive.rb
fanforce-app-factory-2.0.0.rc37 lib/fanforce/app_factory/sinatra/helpers/ractive.rb
fanforce-app-factory-2.0.0.rc36 lib/fanforce/app_factory/sinatra/helpers/ractive.rb
fanforce-app-factory-2.0.0.rc35 lib/fanforce/app_factory/sinatra/helpers/ractive.rb
fanforce-app-factory-2.0.0.rc34 lib/fanforce/app_factory/sinatra/helpers/ractive.rb
fanforce-app-factory-2.0.0.rc33 lib/fanforce/app_factory/sinatra/helpers/ractive.rb
fanforce-app-factory-2.0.0.rc32 lib/fanforce/app_factory/sinatra/helpers/ractive.rb
fanforce-app-factory-2.0.0.rc31 lib/fanforce/app_factory/sinatra/helpers/ractive.rb
fanforce-app-factory-2.0.0.rc30 lib/fanforce/app_factory/sinatra/helpers/ractive.rb
fanforce-app-factory-2.0.0.rc29 lib/fanforce/app_factory/sinatra/helpers/ractive.rb
fanforce-app-factory-2.0.0.rc27 lib/fanforce/app_factory/sinatra/helpers/ractive.rb
fanforce-app-factory-2.0.0.rc26 lib/fanforce/app_factory/sinatra/helpers/ractive.rb
fanforce-app-factory-2.0.0.rc25 lib/fanforce/app_factory/sinatra/helpers/ractive.rb
fanforce-app-factory-2.0.0.rc24 lib/fanforce/app_factory/sinatra/helpers/ractive.rb
fanforce-app-factory-2.0.0.rc23 lib/fanforce/app_factory/sinatra/helpers/ractive.rb