Module: Muj
- Defined in:
- lib/muj.rb
Defined Under Namespace
Classes: Partials
Class Method Summary collapse
- .escape_template(v) ⇒ Object
- .eval(str, json, opts = {}) ⇒ Object
- .render(str, locals = {}, opts = {}) ⇒ Object
- .render_with_json(str, json, opts = {}) ⇒ Object
- .render_with_yaml(str, yml, opts = {}) ⇒ Object
Class Method Details
.escape_template(v) ⇒ Object
40 41 42 43 44 |
# File 'lib/muj.rb', line 40 def self.escape_template(v) v. gsub("\n",'\n'). gsub('"','\"') end |
.eval(str, json, opts = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/muj.rb', line 46 def self.eval(str,json,opts={}) unless (defined? ::Rhino) || (defined? ::V8) begin require 'rhino' rescue LoadError begin require 'v8' rescue LoadError raise LoadError, "V8 (therubyracer) or Rhino (therubyrhino) needed." end end end if (defined?(::Rhino) && defined?(::Rhino::Context)) cxt = ::Rhino::Context.new elsif (defined? ::V8) cxt = ::V8::Context.new end mustachejs_file = File.open(File.dirname(__FILE__)+"/mustache.js") cxt.eval(mustachejs_file.read) mustachejs_file.close 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
77 78 79 |
# File 'lib/muj.rb', line 77 def self.render(str,locals={},opts={}) self.eval(str,locals.to_json,opts) end |
.render_with_json(str, json, opts = {}) ⇒ Object
81 82 83 |
# File 'lib/muj.rb', line 81 def self.render_with_json(str,json,opts={}) self.eval(str,json,opts) end |
.render_with_yaml(str, yml, opts = {}) ⇒ Object
85 86 87 |
# File 'lib/muj.rb', line 85 def self.render_with_yaml(str,yml,opts={}) self.render(str,YAML::load(yml),opts) end |