Sha256: d805a9fd5148013aecb7ec82398d379ed2c9909f9e8c6c90318d322ee80591fc

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

require 'erb'
require 'tilt'

module Angelo
  module Tilt
    module ERB

      # hrm, sneaky
      #
      def self.included base
        base.extend ClassMethods
      end

      module ClassMethods

        DEFAULT_LAYOUT = 'layout.html.erb'

        def view_glob *glob
          File.join view_dir, *glob
        end

        def templatify *glob
          Dir[view_glob *glob].reduce({}) do |h,v|
            sym = v.gsub view_dir + '/', ''
            return h if (block_given? && yield(v))
            sym.gsub! '/', '_'
            sym.gsub! /\.\w+?\.erb$/, ''
            h[sym.to_sym] = ::Tilt::ERBTemplate.new v
            h
          end
        end

        def templates
          @templates ||= templatify('**', '*.erb'){|v| v =~ /^layouts\//}
          @templates
        end

        def layout_templates
          @layout_templates ||= templatify 'layouts', '*.erb'
          @layout_templates
        end

        def default_layout
          if @default_layout.nil?
            l = view_glob(DEFAULT_LAYOUT)
            @default_layout = ::Tilt::ERBTemplate.new l if File.exist? l
          end
          @default_layout
        end

      end

      def erb view, opts = {locals: {}}
        locals = Hash === opts[:locals] ? opts[:locals] : {}
        render = ->{self.class.templates[view].render self, locals}
        case opts[:layout]
        when false
          render[]
        when Symbol
          if lt = self.class.layout_templates[opts[:layout]]
            lt.render self, locals, &render
          else
            raise ArgumentError.new "unknown layout - :#{opts[:layout]}"
          end
        else
          if self.class.default_layout
            self.class.default_layout.render self, locals, &render
          else
            render[]
          end
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
angelo-0.1.0 lib/angelo/tilt/erb.rb
angelo-0.0.9 lib/angelo/tilt/erb.rb