Class: Jader::Compiler

Inherits:
Object show all
Defined in:
lib/jader/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Compiler

Initialize Jader template compiler. @see github.com/visionmedia/jade#options

Parameters:

  • options (Hash) (defaults to: {})

    Jade compiler options

Options Hash (options):

  • :client (Boolean)

    run Jade compiler in client / server mode (default ‘true`)

  • :compileDebug (Boolean)

    run Jade compiler in debug mode(default ‘false`)



11
12
13
14
15
16
# File 'lib/jader/compiler.rb', line 11

def initialize(options={})
  @options = {
    :client => true,
    :compileDebug => false
  }.merge options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/jader/compiler.rb', line 5

def options
  @options
end

Instance Method Details

#compile(template, file_name = '') ⇒ String

Compile a Jade template for client-side use with JST

Parameters:

  • template (String, File)

    Jade template file or text to compile

  • file_name (String) (defaults to: '')

    name of template file used to resolve mixins inclusion

Returns:

  • (String)

    Jade template compiled into Javascript and wrapped inside an anonymous function for 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_versionString

Jade Javascript engine version

Returns:

  • (String)

    version of Jade javascript engine installed in ‘vendor/assets/javascripts`



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

Parameters:

  • template (String)

    Jade template text to render

  • controller_name (String)

    name of Rails controller that’s rendering the template

  • vars (Hash) (defaults to: {})

    controller instance variables passed to the template

Returns:

  • (String)

    HTML output of compiled Jade template



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

#sourceString

Jade template engine Javascript source code used to compile templates in ExecJS

Returns:

  • (String)

    Jade source code



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

Parameters:

  • controller_name (String)

    name of Rails controller rendering a Jade template

Returns:

  • (Array<String>)

    array of Jade mixins to use with a Jade template rendered by a Rails 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

Yields:

  • (context)

    V8::Context compiled Jade source code in V8 context



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