Sha256: cf49b2eb3bcab86c68de74b576bc4fc3732ead111ab64e033ff3cbc3a8265e6a

Contents?: true

Size: 1.29 KB

Versions: 21

Compression:

Stored size: 1.29 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)
    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

21 entries across 21 versions & 1 rubygems

Version Path
fanforce-plugin-factory-2.0.0.rc15 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-2.0.0.rc13 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-2.0.0.rc12 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-2.0.0.rc11 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-2.0.0.rc10 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-2.0.0.rc9 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-2.0.0.rc8 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-2.0.0.rc7 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-2.0.0.rc6 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-2.0.0.rc5 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-2.0.0.rc3 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-1.6.1 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-2.0.0.rc2 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-2.0.0.rc1 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-1.6.0 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-1.6.0.rc6 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-1.6.0.rc5 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-1.6.0.rc4 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-1.6.0.rc3 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb
fanforce-plugin-factory-1.6.0.rc2 lib/fanforce/plugin_factory/sinatra/helpers/ractive.rb