Sha256: f36aac3b12057cac086115cad179e110ea8d5fa3e05d0d3b68a66662b3a24940

Contents?: true

Size: 1.84 KB

Versions: 17

Compression:

Stored size: 1.84 KB

Contents

module HealthDataStandards
  module Export
    # Used to actually render stuff. A RenderingContext needs to be set up with
    # a template helper and may be provided with extensions.
    #
    # :call-seq:
    #   template_helper = HealthDataStandards::Export::TemplateHelper.new('cat1', 'cat1')
    #   rendering_context = HealthDataStandards::Export::RenderingContext.new
    #   rendering_context.template_helper = template_helper
    #   rendering_context.extensions = [HealthDataStandards::Export::Helper::Cat1ViewHelper]
    #
    # In this case, a RenderingContext is being set up to generate Category 1 files. It is
    # passed a template helper to finds the correct ERb templates to render from. It is also
    # given an extension. This is just a Ruby Module which will have its methods exposed to
    # the templates. RenderingContext will assume that extensions is an Array and will include
    # multiple extensions if more than one is provided.  
    class RenderingContext < OpenStruct
      include ViewHelper
      attr_accessor :template_helper, :extensions

      def my_binding
        binding
      end

      def render(params)
        erb = nil
        if params[:template]
          erb = @template_helper.template(params[:template])
        elsif params[:partial]
          erb = @template_helper.partial(params[:partial])
        end
        
        locals = params[:locals]
        locals ||= {}
        rendering_context = RenderingContext.new(locals)
        rendering_context.template_helper = @template_helper
        if @extensions.present?
          rendering_context.extensions = @extensions
          @extensions.each do |extension|
            rendering_context.extend(extension)
          end
        end
        eruby = Erubis::EscapedEruby.new(erb) # TODO: cache these
        eruby.result(rendering_context.my_binding)
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
health-data-standards-3.2.11 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.2.10 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.2.8 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.2.7 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.2.6 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.2.5 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.2.4 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.2.3 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.2.2 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.2.1 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.2.0 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.1.1 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.1.0 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.0.6 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.0.5 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.0.4 lib/health-data-standards/export/rendering_context.rb
health-data-standards-3.0.3 lib/health-data-standards/export/rendering_context.rb