class Smolbars::Context
Public Class Methods
new(**kwargs)
click to toggle source
# File lib/smolbars/context.rb, line 7 def initialize(**kwargs) @js = MiniRacer::Context.new(kwargs) @js.load(Handlebars::Source.bundled_path) end
Public Instance Methods
compile(template)
click to toggle source
Note that this is a hacky JS expression builder. We cannot pass JS AST in to mini_racer so we have to hope the template passed in does not form invalid Ruby. So don’t use templates with backtick characters without manually escaping them
# File lib/smolbars/context.rb, line 15 def compile(template) handle = fn_handle invocation = %Q{var #{handle} = Handlebars.compile(`#{template.gsub('`', "\`")}`);} @js.eval(invocation) ::Smolbars::Template.new(self, handle) end
eval(*args)
click to toggle source
# File lib/smolbars/context.rb, line 22 def eval(*args) @js.eval(*args) end
load(path)
click to toggle source
# File lib/smolbars/context.rb, line 30 def load(path) @js.load(path) end
load_pattern(pattern)
click to toggle source
# File lib/smolbars/context.rb, line 26 def load_pattern(pattern) Dir[pattern].each{ |path| load(path) } end
Private Instance Methods
fn_handle()
click to toggle source
# File lib/smolbars/context.rb, line 36 def fn_handle "js_fn_#{SecureRandom.hex}" end