Class: DryHamlHandlebars::Runner
- Inherits:
-
Object
- Object
- DryHamlHandlebars::Runner
- Defined in:
- lib/dry_haml_handlebars/handler.rb
Instance Method Summary collapse
- #compile_hbs ⇒ Object
- #gen_partial_loader ⇒ Object
- #gen_template_loader ⇒ Object
- #get_hbs_context ⇒ Object
-
#initialize(template, rendered_haml = nil, view_type, view_name, partial_name, relative_view_path, rabl_path, template_path, compiled_template_path) ⇒ Runner
constructor
A new instance of Runner.
- #load_template ⇒ Object
- #name_partial ⇒ Object
- #name_template ⇒ Object
- #render_content_for(name, path) ⇒ Object
- #render_handlebars_partial_command ⇒ Object
- #render_rabl ⇒ Object
- #render_template ⇒ Object
- #run ⇒ Object
- #set_gon_variable ⇒ Object
- #set_locale ⇒ Object
- #write_asset_files ⇒ Object
Constructor Details
#initialize(template, rendered_haml = nil, view_type, view_name, partial_name, relative_view_path, rabl_path, template_path, compiled_template_path) ⇒ Runner
Returns a new instance of Runner.
184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/dry_haml_handlebars/handler.rb', line 184 def initialize(template, rendered_haml = nil, view_type, view_name, partial_name, relative_view_path, rabl_path, template_path, compiled_template_path) @template = template @rendered_haml = rendered_haml @view_type = view_type @view_name = view_name @partial_name = partial_name @relative_view_path = relative_view_path @rabl_path = rabl_path @template_path = template_path @compiled_template_path = compiled_template_path end |
Instance Method Details
#compile_hbs ⇒ Object
271 272 273 274 275 |
# File 'lib/dry_haml_handlebars/handler.rb', line 271 def compile_hbs <<-RUBY compiled_hbs = HandlebarsAssets::Handlebars.precompile( rendered_haml ) RUBY end |
#gen_partial_loader ⇒ Object
300 301 302 303 304 305 306 |
# File 'lib/dry_haml_handlebars/handler.rb', line 300 def gen_partial_loader <<-'RUBY' hbs_loader = "(function() { this.Handlebars.registerPartial('#{partial_name}', Handlebars.template(#{compiled_hbs})); }).call(this)" RUBY end |
#gen_template_loader ⇒ Object
290 291 292 293 294 295 296 297 298 |
# File 'lib/dry_haml_handlebars/handler.rb', line 290 def gen_template_loader <<-'RUBY' hbs_loader = "(function() { this.HandlebarsTemplates || (this.HandlebarsTemplates = {}); this.HandlebarsTemplates['#{template_names.last}'] = Handlebars.template(#{compiled_hbs}); return HandlebarsTemplates['#{template_names.last}']; }).call(this)" RUBY end |
#get_hbs_context ⇒ Object
315 316 317 318 319 |
# File 'lib/dry_haml_handlebars/handler.rb', line 315 def get_hbs_context <<-RUBY hbs_context = HandlebarsAssets::Handlebars.send(:context) RUBY end |
#load_template ⇒ Object
327 328 329 330 331 332 333 |
# File 'lib/dry_haml_handlebars/handler.rb', line 327 def load_template <<-RUBY File.open('#{@compiled_template_path}') do |file| hbs_context.eval(file.read, '#{@view_name}.js') end RUBY end |
#name_partial ⇒ Object
284 285 286 287 288 |
# File 'lib/dry_haml_handlebars/handler.rb', line 284 def name_partial <<-RUBY partial_name = '#{@partial_name}' RUBY end |
#name_template ⇒ Object
277 278 279 280 281 282 |
# File 'lib/dry_haml_handlebars/handler.rb', line 277 def name_template <<-RUBY template_names ||= [] template_names << '#{File.join(@relative_view_path, @view_name).to_s}' RUBY end |
#render_content_for(name, path) ⇒ Object
380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/dry_haml_handlebars/handler.rb', line 380 def render_content_for(name, path) haml_handler = ActionView::Template.handler_for_extension :haml haml_source = File.read(path) haml_template = ActionView::Template.new(haml_source, path, haml_handler, {}) haml_call = haml_handler.call haml_template <<-RUBY @view_flow.set( :#{name}, eval(%q( #{haml_call} )).html_safe ) RUBY end |
#render_handlebars_partial_command ⇒ Object
374 375 376 377 378 |
# File 'lib/dry_haml_handlebars/handler.rb', line 374 def <<-RUBY '{{> #{@partial_name}}}'.html_safe RUBY end |
#render_rabl ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/dry_haml_handlebars/handler.rb', line 335 def render_rabl if File.exist? @rabl_path rabl_handler = ActionView::Template.handler_for_extension :rabl rabl_source = File.read(@rabl_path) rabl_template = ActionView::Template.new(rabl_source, @rabl_path, rabl_handler, {:locals => @template.locals}) rabl_call = rabl_handler.call rabl_template <<-RUBY rabl_call_str = %q( #{rabl_call} ) rendered_rabl = eval(rabl_call_str).html_safe RUBY else <<-RUBY rendered_rabl ||= '{}'.html_safe RUBY end end |
#render_template ⇒ Object
367 368 369 370 371 372 |
# File 'lib/dry_haml_handlebars/handler.rb', line 367 def render_template <<-'RUBY' current_template_name = template_names.pop hbs_context.eval( "HandlebarsTemplates['#{current_template_name}'](#{rendered_rabl})" ) RUBY end |
#run ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/dry_haml_handlebars/handler.rb', line 196 def run content_cache = DryHamlHandlebars.content_cache out = [] if @rendered_haml out << @rendered_haml out << compile_hbs case @view_type when :template out << name_template out << gen_template_loader when :partial out << name_partial out << gen_partial_loader end out << write_asset_files out << get_hbs_context out << load_template else #if we don't have any rendered haml (we're probably in production) out << get_hbs_context out << name_template end out << set_locale if defined? SimplesIdeias::I18n case @view_type when :template out << render_rabl out << set_gon_variable if content_cache.store.present? #do we need to render some content for another view? content_cache.store.each do |item| name = item.name path = item.path content_cache.remove_item(item) #NOTE: this call will overwrite all eval'd variables set below, except for template_names #we store this in a stack and pop the last name when we get to render_template() #it doesn't matter about the other variables as we're finished with them by this stage #and it keeps the eval code simpler if we just reuse them out << render_content_for(name, path) end content_cache.clear #just to be sure end out << render_template when :partial out << end out.join("\n") end |
#set_gon_variable ⇒ Object
359 360 361 362 363 364 365 |
# File 'lib/dry_haml_handlebars/handler.rb', line 359 def set_gon_variable <<-'RUBY' Gon::Request.id = request.object_id Gon::Request.env = request.env Gon.view_data ||= JSON.parse(rendered_rabl) RUBY end |
#set_locale ⇒ Object
321 322 323 324 325 |
# File 'lib/dry_haml_handlebars/handler.rb', line 321 def set_locale <<-RUBY hbs_context.eval("I18n.locale = '#{I18n.locale.to_s}'") RUBY end |
#write_asset_files ⇒ Object
308 309 310 311 312 313 |
# File 'lib/dry_haml_handlebars/handler.rb', line 308 def write_asset_files <<-RUBY File.open('#{@template_path}', 'w+') {|f| f.write(rendered_haml) } File.open('#{@compiled_template_path}', 'w+') {|f| f.write(hbs_loader) } RUBY end |