Sha256: 01d2e8c657c4139f835d900b0b7db56cc69b252c957c05896670dbd7da5824e3

Contents?: true

Size: 955 Bytes

Versions: 1

Compression:

Stored size: 955 Bytes

Contents

module Brochure
  class Context
    include Tilt::CompileSite

    def self.for(helpers)
      context = Class.new(self)
      context.send(:include, *helpers) if helpers.any?
      context
    end

    attr_reader :application, :template, :env

    def initialize(application, template, env, assigns = {})
      @application = application
      @template = template
      @env = env

      load_assigns(assigns)
    end

    def load_assigns(assigns)
      assigns.each do |name, value|
        instance_variable_set("@#{name}", value)
      end
    end

    def request
      @request ||= Rack::Request.new(env)
    end

    def h(html)
      Rack::Utils.escape_html(html)
    end

    def render(logical_path, locals = {})
      if partial = application.find_partial(logical_path, template.format_extension)
        partial.render(env, locals)
      else
        raise TemplateNotFound, "no such template '#{logical_path}'"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
brochure-0.4.0 lib/brochure/context.rb