Sha256: 86303badb604c6a213fd7e843ffcda8cf8d3fa7a91df1d842ec8f1f418f13d3d

Contents?: true

Size: 1.31 KB

Versions: 6

Compression:

Stored size: 1.31 KB

Contents

require "tilt"

class Cuba
  module Render
    def self.setup(app)
      app.settings[:template_engine] ||= "erb"
      app.settings[:views] ||= File.expand_path("views", Dir.pwd)
    end

    def view(template, locals = {}, layout = "layout")
      partial(layout, { content: partial(template, locals) }.merge(locals))
    end

    def partial(template, locals = {})
      render("#{settings[:views]}/#{template}.#{settings[:template_engine]}",
        locals, default_encoding: Encoding.default_external)
    end

    # Render any type of template file supported by Tilt.
    #
    # @example
    #
    #   # Renders home, and is assumed to be HAML.
    #   render("home.haml")
    #
    #   # Renders with some local variables
    #   render("home.haml", site_name: "My Site")
    #
    #   # Renders with HAML options
    #   render("home.haml", {}, ugly: true, format: :html5)
    #
    #   # Renders in layout
    #   render("layout.haml") { render("home.haml") }
    #
    def render(template, locals = {}, options = {}, &block)
      _cache.fetch(template) {
        Tilt.new(template, 1, options)
      }.render(self, locals, &block)
    end

    # @private Used internally by #render to cache the
    #          Tilt templates.
    def _cache
      Thread.current[:_cache] ||= Tilt::Cache.new
    end
    private :_cache
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cuba-3.0.1.rc1 lib/cuba/render.rb
cuba-3.0.0 lib/cuba/render.rb
cuba-3.0.0.rc5 lib/cuba/render.rb
cuba-3.0.0.rc4 lib/cuba/render.rb
cuba-3.0.0.rc3 lib/cuba/render.rb
cuba-3.0.0.rc2 lib/cuba/render.rb