Sha256: fc7dac3ae673f50384d19bc586f4d01e8f370343cafaabc5c71ffe64cf2b8c06

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

require "tilt"

module Tynn::Render
  def self.setup(app, options = {}) # :nodoc:
    options = options.dup

    options[:engine]  ||= "erb"
    options[:layout]  ||= "layout"
    options[:views]   ||= File.expand_path("views", Dir.pwd)

    options[:options] ||= {}
    options[:options] = {
      default_encoding: Encoding.default_external,
      outvar: "@_output"
    }.merge!(options[:options])

    app.settings[:render] ||= options
  end

  module ClassMethods
    def layout(layout)
      settings[:render][:layout] = layout
    end
  end

  def render(template, locals = {}, layout = settings[:render][:layout])
    res.headers[Rack::CONTENT_TYPE] ||= Syro::Response::DEFAULT

    res.write(view(template, locals, layout))
  end

  def view(template, locals = {}, layout = settings[:render][:layout])
    return partial(layout, locals.merge(content: partial(template, locals)))
  end

  def partial(template, locals = {})
    return tilt(template_path(template), locals, settings[:render][:options])
  end

  private

  def tilt(file, locals = {}, opts = {})
    return tilt_cache.fetch(file) { Tilt.new(file, 1, opts) }.render(self, locals)
  end

  def tilt_cache
    return Thread.current[:tilt_cache] ||= Tilt::Cache.new
  end

  def template_path(template)
    dir = settings[:render][:views]
    ext = settings[:render][:engine]

    return File.join(dir, "#{ template }.#{ ext }")
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tynn-1.0.0.rc1 lib/tynn/render.rb
tynn-0.0.4 lib/tynn/render.rb
tynn-0.0.3 lib/tynn/render.rb
tynn-0.0.2 lib/tynn/render.rb