Sha256: 3c1a9ff95c438b64284a96a7b9ea55ff4104a40cf14865e7c4d42a85899d1583

Contents?: true

Size: 1.71 KB

Versions: 12

Compression:

Stored size: 1.71 KB

Contents

module Sinatra::RactiveHelpers

  # Creates a string representation of a javascript object for ractive.js
  def compile_ractive_templates(options={}, &block)
    context = Object.new
    class << context
      include Haml::Helpers
    end
    context.init_haml_helpers

    format = options[:format] == 'html' ? :html : :json

    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)

    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 = Object.new
    class << context
      include Haml::Helpers
    end
    context.init_haml_helpers
    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 column(size, &block)
    begin require 'haml' rescue LoadError raise 'You must have the haml gem installed to use Fanforce.compile_jquery_templates.' end
    if block
      context = Object.new
      class << context
        include Haml::Helpers
      end
      context.init_haml_helpers
      html = context.capture_haml(&block)
    end
    "<div class='small-12 medium-#{size} column'>\n#{html}</div>"
  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
fanforce-app-factory-1.7.1 lib/fanforce/app_factory/config/helpers/ractive.rb
fanforce-app-factory-1.7.0 lib/fanforce/app_factory/config/helpers/ractive.rb
fanforce-app-factory-1.6.3 lib/fanforce/app_factory/config/helpers/ractive.rb
fanforce-app-factory-1.6.2 lib/fanforce/app_factory/config/helpers/ractive.rb
fanforce-app-factory-1.6.1 lib/fanforce/app_factory/config/helpers/ractive.rb
fanforce-app-factory-1.6.0 lib/fanforce/app_factory/config/helpers/ractive.rb
fanforce-app-factory-1.5.0 lib/fanforce/app_factory/config/helpers/ractive.rb
fanforce-app-factory-1.4.4 lib/fanforce/app_factory/config/helpers/ractive.rb
fanforce-app-factory-1.4.3 lib/fanforce/app_factory/config/helpers/ractive.rb
fanforce-app-factory-1.4.2 lib/fanforce/app_factory/config/helpers/ractive.rb
fanforce-app-factory-1.4.1 lib/fanforce/app_factory/config/helpers/ractive.rb
fanforce-app-factory-1.4.0 lib/fanforce/app_factory/config/helpers/ractive.rb