Sha256: 995382c54bf146ed434911266fc014e6f47ed64b493461874dfb757a0495e73a

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

require 'tilt'

module ActionTree::Components::Tilt

  CONFIG = {:template_path => 'views'}

  module NodeMethods
    def templates
      @templates ||= {}
    end

    def template(*args)
      tilt    = args.find {|a| a.is_a?(Tilt::Template) }
      name    = args.find {|a| a.is_a?(Symbol) }
      source  = args.find {|a| a.is_a?(String) }
      options = args.find {|a| a.is_a?(Hash) }

      unless tilt
        path = File.join(@conf[:template_path], source)
        tilt = Tilt.new(path, nil, options)
      end
      templates[name] = tilt
    end
  end

  module QueryMethods
    
    # Hash of templates like :name => [tmpl1, tmpl2, ...]
    def templates
      @templates ||= node_chain.inject({}) do |hsh, node|
        hsh[name] ||= []
        hsh[name] << node.templates[name]
      end
    end
  end

  module Helpers
    
    # Public: Renders a template
    def render(*args, &blk)
      tilt    = args.find {|a| a.is_a?(Tilt::Template) }
      name    = args.find {|a| a.is_a?(Symbol) }
      source  = args.find {|a| a.is_a?(String) }
      options = args.find {|a| a.is_a?(Hash) }

      if tilt && source
        path = File.join(config[:template_path], source)
        tilt = Tilt.new(path, nil, options)
      end

      templates = @_match.templates[name]

      default :type, templates.first.class.default_mime_type

      result =  tilt ? tilt.render(self, options, &blk) : nil

      templates.reverse.inject(result) do |r, template|
        template.render(self) { r }
      end
    end
  end
end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
action_tree-0.2.0 lib/action_tree/components/tilt.rb