Sha256: ae02ccf353d38c8a965593f287ddace28b7c41449b3517ae104433e8e51d6ecc

Contents?: true

Size: 1.3 KB

Versions: 30

Compression:

Stored size: 1.3 KB

Contents

require 'alula/theme/view'
require 'tilt'
require 'sass'  # As suggested by tilt

module Alula
  class Theme
    class Layout
      attr_reader :theme
      attr_reader :name
      attr_reader :context

      def initialize(opts)
        @theme = opts.delete(:theme)
        @name = opts.delete(:name)
        @file = opts.delete(:file)
        
        @views = {}
        
        @context = @theme.context
        
        @template = Tilt.new(@file, nil, @theme.options(@file))
      end
      
      def view(name)
        @views[name] ||= begin
          # Find our layout name
          file = Dir[::File.join(self.theme.path, "views", "#{name}.*")].first
          if file
            View.new(layout: self, name: name, file: file)
          end
        end
      end
      
      def render(content, &blk)
        _old_values = {}
        
        # Set up context, make sure we don't change anything
        begin
          content.each do |key, value|
            _old_values[key] = self.context[key]
            self.context[key] = value
          end
          
          # Render using our layout template
          @template.render(self.context, &blk)
          
        ensure
          _old_values.each do |key, value|
            self.context[key] = value
          end
        end
      end
      
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
alula-0.4.27 lib/alula/theme/layout.rb
alula-0.4.26 lib/alula/theme/layout.rb
alula-0.4.25 lib/alula/theme/layout.rb
alula-0.4.24 lib/alula/theme/layout.rb
alula-0.4.23 lib/alula/theme/layout.rb
alula-0.4.22 lib/alula/theme/layout.rb
alula-0.4.21 lib/alula/theme/layout.rb
alula-0.4.20 lib/alula/theme/layout.rb
alula-0.4.19 lib/alula/theme/layout.rb
alula-0.4.18 lib/alula/theme/layout.rb
alula-0.4.17 lib/alula/theme/layout.rb
alula-0.4.16 lib/alula/theme/layout.rb
alula-0.4.15 lib/alula/theme/layout.rb
alula-0.4.14 lib/alula/theme/layout.rb
alula-0.4.13 lib/alula/theme/layout.rb
alula-0.4.12 lib/alula/theme/layout.rb
alula-0.4.11 lib/alula/theme/layout.rb
alula-0.4.11a lib/alula/theme/layout.rb
alula-0.4.10 lib/alula/theme/layout.rb
alula-0.4.9 lib/alula/theme/layout.rb