Module: RubyApp::Mixins::RenderMixin

Included in:
Element, Element
Defined in:
lib/ruby_app/mixins/render_mixin.rb

Instance Method Summary collapse

Instance Method Details

#content_for(name, value = nil, &block) ⇒ Object



20
21
22
# File 'lib/ruby_app/mixins/render_mixin.rb', line 20

def content_for(name, value = nil, &block)
  RubyApp::Request.content_for(self, name, value, &block)
end

#render(format, write_cache = false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ruby_app/mixins/render_mixin.rb', line 24

def render(format, write_cache = false)
  cache = self.is_a?(Class) ? self.get_cache(format) : self.class.get_cache(format)
  if File.exists?(cache)
    self.rendered?(cache) do
      return File.read(cache)
    end
  else
    templates = self.is_a?(Class) ? self.get_templates(format) : self.class.get_templates(format)
    unless templates.empty?
      self.init_haml_helpers
      begin
        yield(self) if block_given?
        templates.each_with_index do |template, index|
          content = Haml::Engine.new(File.read(template), :filename => template).render(self) do |*arguments|
            if arguments.empty?
              index == 0 ? nil : RubyApp::Request.get_content(self, templates[index - 1])
            else
              _content = RubyApp::Request.get_content(self, arguments[0])
              if self.block_is_haml?(_content)
                self.capture_haml(arguments, &_content)
              else
                _content
              end
            end
          end
          RubyApp::Request.content_for(self, template, content)
        end
        if cache && write_cache
          cache_directory = File.dirname(cache)
          Dir.mkdir(cache_directory) unless File.exists?(cache_directory)
          File.open(cache, 'w') do |file|
            file.write(RubyApp::Request.get_content(self, templates.last))
            file.flush
          end
        end
        return RubyApp::Request.get_content(self, templates.last)
      ensure
        RubyApp::Request.clear_content(self)
      end
    end
  end
end

#rendered?(template) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
# File 'lib/ruby_app/mixins/render_mixin.rb', line 13

def rendered?(template)
  unless RubyApp::Request.rendered?(template)
    yield
    RubyApp::Request.rendered(template)
  end
end