Sha256: 6cd7e25386ead130fd07605ac3d9081781904ffaf4d0f09f4806dddf68341d57

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents



require 'tilt'

module ActionTree::Plugins::Tilt

  CACHE = Tilt::Cache.new

  module NodeMixin
    # sets a default, optionally namespaced, layout for this and
    # descending nodes
    def layout(path, namespace=nil)
      layout_namespaces[namespace] = path
    end

    def layout_namespaces
      @layout_namespaces ||= {}
    end

    # sets a default view folder path for this and descending nodes.
    def view_path(path=nil)
      path ? @view_path = path : @view_path
    end
  end

  module MatchMixin
    def view_path
      inherit_via_node_chain(&:view_path) || '.'
    end

    def layout(namespace=nil)
      inherit_via_node_chain do |node|
        node.layout_namespaces[namespace]
      end
    end
  end


  module Helpers

    def render(path, layout_namespace=nil, &blk)
      path = ::File.join(@__match.view_path, path)
      
      template = ::ActionTree::Plugins::Tilt::CACHE.fetch(
        path) { Tilt.new(path, nil, {}) }
      
      if respond_to?(:content_type)
        content_type(template.class.default_mime_type)
      end

      result = template.render(self, &blk)

      layout = @__match.layout(layout_namespace)
      if layout && !%w{sass scss less coffee}.include?(File.extname(path))
        render(layout, :AbSoLuTeLy_No_LaYoUt) { result }
      else
        result
      end
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
action_tree-0.1.1 lib/action_tree/plugins/tilt.rb
action_tree-0.1.0 lib/action_tree/plugins/tilt.rb