Class: Jader::Compiler
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#compile(template, file_name = '') ⇒ String
Compile a Jade template for client-side use with JST.
-
#initialize(options = {}) ⇒ Compiler
constructor
Initialize Jader template compiler.
-
#jade_version ⇒ String
Jade Javascript engine version.
-
#render(template, controller_name, vars = {}) ⇒ String
Compile and evaluate a Jade template for server-side rendering.
-
#source ⇒ String
Jade template engine Javascript source code used to compile templates in ExecJS.
-
#template_mixins(controller_name) ⇒ Array<String>
Jade template mixins for a given controller.
-
#v8_context {|context| ... } ⇒ Object
V8 context with Jade code compiled.
Constructor Details
#initialize(options = {}) ⇒ Compiler
Initialize Jader template compiler. @see github.com/visionmedia/jade#options
11 12 13 14 15 16 |
# File 'lib/jader/compiler.rb', line 11 def initialize(={}) @options = { :client => true, :compileDebug => false }.merge end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/jader/compiler.rb', line 5 def @options end |
Instance Method Details
#compile(template, file_name = '') ⇒ String
Compile a Jade template for client-side use with JST
50 51 52 53 54 55 56 57 58 |
# File 'lib/jader/compiler.rb', line 50 def compile(template, file_name = '') v8_context do |context| template = template.read if template.respond_to?(:read) file_name.match(/views\/([^\/]+)\//) controller_name = $1 || nil combo = (template_mixins(controller_name) << template).join("\n").to_json context.eval("jade.compile(#{combo},#{@options.to_json})").to_s.sub('function anonymous','function') end end |
#jade_version ⇒ String
Jade Javascript engine version
40 41 42 43 44 |
# File 'lib/jader/compiler.rb', line 40 def jade_version v8_context do |context| context.eval("jade.version") end end |
#render(template, controller_name, vars = {}) ⇒ String
Compile and evaluate a Jade template for server-side rendering
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/jader/compiler.rb', line 65 def render(template, controller_name, vars = {}) v8_context do |context| context.eval(Jader.configuration.includes.join("\n")) combo = (template_mixins(controller_name) << template).join("\n").to_json context.eval("var fn = jade.compile(#{combo})") context.eval("fn(#{vars.to_jade.to_json})") end #tmpl = context.eval("jade.precompile(#{combo}, #{@options.to_json})") #context.eval(%{ # function(locals){ # #{Jader::Source::runtime} # #{Jader.configuration.includes.join("\n")} # #{tmpl} # }.call(null,#{vars.to_jade.to_json}) #}) end |
#source ⇒ String
Jade template engine Javascript source code used to compile templates in ExecJS
20 21 22 23 24 25 26 |
# File 'lib/jader/compiler.rb', line 20 def source @source ||= %{ var window = {}; #{Jader::Source::jade} var jade = window.jade; } end |
#template_mixins(controller_name) ⇒ Array<String>
Jade template mixins for a given controller
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/jader/compiler.rb', line 85 def template_mixins(controller_name) mixins = [] unless Jader.configuration.mixins_path.nil? Dir["#{Jader.configuration.mixins_path}/*.jade"].each do |f| base_name = File.basename(f) if base_name == '%s_mixins.jade' % controller_name || base_name == 'application_mixins.jade' mixins << IO.read(f) end end end mixins end |
#v8_context {|context| ... } ⇒ Object
V8 context with Jade code compiled
30 31 32 33 34 35 36 |
# File 'lib/jader/compiler.rb', line 30 def v8_context V8::C::Locker() do context = V8::Context.new context.eval(source) yield context end end |