Class: Fortitude::Tilt::FortitudeTemplate

Inherits:
Tilt::Template
  • Object
show all
Defined in:
lib/fortitude/tilt/fortitude_template.rb

Instance Method Summary collapse

Instance Method Details

#prepareObject


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fortitude/tilt/fortitude_template.rb', line 9

def prepare
  if file && line
    ::Object.class_eval(data, file, line)
  else
    ::Object.class_eval(data)
  end

  # 2014-06-19 ageweke -- Earlier versions of Tilt try to instantiate the engine with an empty tempate as a way
  # of making sure it can be created, so we have to support this case.
  if data.strip.length > 0
    @fortitude_class = ::Fortitude::Widget.widget_class_from_source(
      data,
      :magic_comment_text => 'fortitude_tilt_class',
      :class_names_to_try => Array(options[:fortitude_class]) + Array(options[:class_names_to_try]))
  end
end

#render(scope = Object.new, locals = nil, &block) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fortitude/tilt/fortitude_template.rb', line 26

def render(scope=Object.new, locals = nil, &block)
  locals ||= { }

  rendering_context = Fortitude::RenderingContext.new({
    :yield_block => block, :render_yield_result => false,
    :helpers_object => scope, :instance_variables_object => scope })

  widget_assigns = { }

  scope.instance_variables.each do |instance_variable_name|
    if instance_variable_name.to_s =~ /^\@(.*)$/
      widget_assigns[$1] = scope.instance_variable_get(instance_variable_name)
    end
  end

  widget_assigns = widget_assigns.merge(locals)
  widget_assigns = fortitude_class.extract_needed_assigns_from(widget_assigns) unless fortitude_class.extra_assigns == :use

  widget = fortitude_class.new(widget_assigns)
  widget.render_to(rendering_context)
  rendering_context.output_buffer_holder.output_buffer
end