Class: Alula::Theme::Layout

Inherits:
Object
  • Object
show all
Defined in:
lib/alula/theme/layout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Layout

Returns a new instance of Layout.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/alula/theme/layout.rb', line 12

def initialize(opts)
  @theme = opts.delete(:theme)
  @name = opts.delete(:name)
  @file = opts.delete(:file)
  
  @views = {}
  
  @context = @theme.context
  
  @template = Tilt.new(@file, nil, @theme.options(@file))
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



10
11
12
# File 'lib/alula/theme/layout.rb', line 10

def context
  @context
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/alula/theme/layout.rb', line 9

def name
  @name
end

#themeObject (readonly)

Returns the value of attribute theme.



8
9
10
# File 'lib/alula/theme/layout.rb', line 8

def theme
  @theme
end

Instance Method Details

#render(content, &blk) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/alula/theme/layout.rb', line 34

def render(content, &blk)
  _old_values = {}
  
  # Set up context, make sure we don't change anything
  begin
    content.each do |key, value|
      _old_values[key] = self.context[key]
      self.context[key] = value
    end
    
    # Render using our layout template
    @template.render(self.context, &blk)
    
  ensure
    _old_values.each do |key, value|
      self.context[key] = value
    end
  end
end

#view(name) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/alula/theme/layout.rb', line 24

def view(name)
  @views[name] ||= begin
    # Find our layout name
    file = Dir[::File.join(self.theme.path, "views", "#{name}.*")].first
    if file
      View.new(layout: self, name: name, file: file)
    end
  end
end