Module: Muj

Defined in:
lib/muj.rb

Defined Under Namespace

Classes: Partials

Class Method Summary collapse

Class Method Details

.escape_template(v) ⇒ Object



40
41
42
# File 'lib/muj.rb', line 40

def self.escape_template(v)
  v.chomp.gsub("\n",'\n\\').gsub('"','\"')
end

.eval(str, json, opts = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/muj.rb', line 44

def self.eval(str,json,opts={})
  require 'v8' unless defined? ::V8
  cxt = ::V8::Context.new
  cxt.load(File.dirname(__FILE__)+"/mustache.js")
  opts['views'] = '.' unless opts['views']
  cxt['partials'] = Muj::Partials.new(opts['views'])
  cxt.eval("var locals = #{json};")
  r = cxt.eval(%Q{locals.__yield__ = Mustache.to_html("#{Muj.escape_template(str)}", locals, partials);})
  if opts['layout']
    layout = cxt['partials'].__read(opts['layout'])
    r = cxt.eval(%Q{Mustache.to_html("#{Muj.escape_template(layout)}", locals, partials);})
  end
  r
end

.render(str, locals = {}, opts = {}) ⇒ Object



59
60
61
# File 'lib/muj.rb', line 59

def self.render(str,locals={},opts={})
  self.eval(str,locals.to_json,opts)
end

.render_with_json(str, json, opts = {}) ⇒ Object



63
64
65
# File 'lib/muj.rb', line 63

def self.render_with_json(str,json,opts={})
  self.eval(str,json,opts)
end

.render_with_yaml(str, yml, opts = {}) ⇒ Object



67
68
69
# File 'lib/muj.rb', line 67

def self.render_with_yaml(str,yml,opts={})
  self.render(str,YAML::load(yml),opts)
end