Class: Presently::SlideView

Inherits:
Live::View
  • Object
show all
Defined in:
lib/presently/slide_view.rb

Overview

Renders a single slide using its XRB template.

Loads templates from the configured templates root and renders slides by passing their content sections to the template via TemplateScope.

Instance Method Summary collapse

Constructor Details

#initialize(id = Live::Element.unique_id, data = {}, css_class: "slide", controller: nil) ⇒ SlideView

Initialize a new slide view.



24
25
26
27
28
29
# File 'lib/presently/slide_view.rb', line 24

def initialize(id = Live::Element.unique_id, data = {}, css_class: "slide", controller: nil)
	super(id, data)
	@css_class = css_class
	@templates_root = controller&.templates_root || DEFAULT_TEMPLATES_ROOT
	@templates = {}
end

Instance Method Details

#render(builder) ⇒ Object

Render the current slide.



60
61
62
63
# File 'lib/presently/slide_view.rb', line 60

def render(builder)
	slide = nil
	render_slide(builder, slide)
end

#render_slide(builder, slide, extra_class: nil) ⇒ Object

Render a slide using its template into the builder.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/presently/slide_view.rb', line 45

def render_slide(builder, slide, extra_class: nil)
	return unless slide
	
	template = template_for(slide.template)
	scope = TemplateScope.new(slide)
	html = template.to_string(scope)
	
	classes = [@css_class, extra_class].compact.join(" ")
	builder.tag(:div, class: classes, data: {template: slide.template}) do
		builder.raw(html)
	end
end

#template_for(name) ⇒ Object

Load and cache a template by name.



34
35
36
37
38
39
# File 'lib/presently/slide_view.rb', line 34

def template_for(name)
	@templates[name] ||= begin
		path = File.join(@templates_root, "#{name}.xrb")
		XRB::Template.load_file(path)
	end
end