Sha256: 2e7dc09fea38fea88c094beec0994e718883ec002d910a4786f98ce15c61cc04

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'ember'
require 'json'
require 'yaml'

# Renders and compiles Jason templates.
module Jason
  # Render a template.
  # 
  # @example
  #     Jason.render('foo: bar') # => '{"foo": "bar"}'
  # 
  # @param [String] template the template to render
  # @param [Binding] binding the binding to render the template in
  # @return [String] the rendered template
  def self.render(template, binding = nil)
    if binding
      yaml = ember_template(template).render(binding)
    else
      yaml = ember_template(template).render
    end
    
    YAML::load(yaml).to_json
  end
  
  # Compile a template.
  # 
  # Eval the returned value to render the template within the current binding.
  # 
  # @param [String] template the template to compile
  # @return [String] the compiled template
  def self.compile(template)
    "YAML::load(#{ember_template(template).program}).to_json"
  end
  
  private
  
  def self.ember_template(template)
    Ember::Template.new(template,
      :unindent => true,
      :infer_end => true,:shorthand => true
    )
  end
end

require 'jason/version'

if defined? ActionView::Template
  require 'jason/rails_template_handler'
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jason-0.2.0 lib/jason.rb