Sha256: ab309a8c035d8984a8f2c8dc2067b87217507a2d55f7d6724543af7943d14a29

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require 'rubygems'
require 'bundler/setup'

require 'facets/string/interpolate'
require 'haml'

module RubyApp

  module Mixins

    module RenderMixin

      def rendered?(template)
        unless ( Thread.current[:_rendered] ||= {} ).key?(template)
          yield
          Thread.current[:_rendered][template] = ( Thread.current[:_rendered][template] || 0 ) + 1
        end
      end

      def content_for(name, value = nil, &block)
        ( @_content ||= {} )[Thread.current] ||= {}
        if block_given?
          @_content[Thread.current][name] = block
        elsif value
          @_content[Thread.current][name] = String.interpolate { value }
        else
          @_content[Thread.current][name]
        end
      end

      def clear_content_for
        ( @_content ||= {} )[Thread.current] ||= {}
        @_content.delete(Thread.current)
      end

      def render(format)
        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 : self.content_for(templates[index - 1])
                else
                  _content = self.content_for(arguments[0])
                  if self.block_is_haml?(_content)
                    self.capture_haml(arguments, &_content)
                  else
                    _content
                  end
                end
              end
              self.content_for(template, content)
            end
            return self.content_for(templates.last)
          ensure
            self.clear_content_for
          end
        end

      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
RubyApp-0.0.69 lib/ruby_app/mixins/render_mixin.rb