Class: Frontman::Context

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
ForwardCallsToApp
Defined in:
lib/frontman/context.rb

Instance Method Summary collapse

Methods included from ForwardCallsToApp

included, #method_missing, #respond_to_missing?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Frontman::ForwardCallsToApp

Instance Method Details

#content_for(key, content = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/frontman/context.rb', line 40

def content_for(key, content = nil)
  # Haml is not designed to do handle content_for properly
  # so we need to hack the buffer of haml that is set
  # inside the context by haml
  save_buffer

  content ||= ''

  if block_given?
    # We don't save the content of the yield, it will be saved in the buffer
    yield

    # The buffer now contains the content of the yield
    content = load_buffer
  end

  # Restore the buffer so the rendering of the file can continue
  restore_buffer

  # We store the the content block inside the current page
  # because we don't know which renderer/layout/template will need it
  current_page = Frontman::App.instance.current_page
  current_page.content_blocks[key.to_sym] = content unless current_page.nil?
end

#content_for?(key) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/frontman/context.rb', line 66

def content_for?(key)
  Frontman::App.instance.current_page.content_blocks.key?(key.to_sym)
end

#get_binding(&content) ⇒ Object



80
81
82
# File 'lib/frontman/context.rb', line 80

def get_binding(&content)
  binding { content }
end

#wrap_layout(layout) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/frontman/context.rb', line 15

def wrap_layout(layout)
  layout_dir = Frontman::Config.get(:layout_dir, fallback: 'views/layouts')
  layout_path = File.join(layout_dir, layout)

  # Haml is not designed to do handle wrap_layout properly
  # so we need to hack the buffer of haml that is set
  # inside the context by haml
  save_buffer

  # We don't save the content of the yield, it will be saved in the buffer
  yield

  # The buffer now contains the content of the yield
  content = load_buffer

  # Restore the buffer so the rendering of the file can continue
  restore_buffer

  Resource.from_path(layout_path, nil, false).render(content)
end

#yield_content(key, *_args) ⇒ Object



73
74
75
# File 'lib/frontman/context.rb', line 73

def yield_content(key, *_args)
  Frontman::App.instance.current_page.content_blocks[key.to_sym]
end