Module: Middleman::CoreExtensions::Routing::InstanceMethods

Defined in:
lib/middleman-core/core_extensions/routing.rb

Overview

Routing instance methods

Instance Method Summary collapse

Instance Method Details

#page(url, opts = {}, &block) ⇒ void

This method returns an undefined value.

The page method allows the layout to be set on a specific path

page "/about.html", :layout => false
page "/", :layout => :homepage_layout

Parameters:

  • url (String)
  • opts (Hash) (defaults to: {})


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/middleman-core/core_extensions/routing.rb', line 47

def page(url, opts={}, &block)
  blocks = Array(block)

  # Default layout
  opts[:layout] = layout if opts[:layout].nil?

  # If the url is a regexp
  if url.is_a?(Regexp) || url.include?("*")

    # Use the metadata loop for matching against paths at runtime
    sitemap. url do |url|
      { :options => opts, :blocks => blocks }
    end

    return
  end

  # Normalized path
  url = '/' + Middleman::Util.normalize_path(url)
  if url.end_with?('/') || File.directory?(File.join(source_dir, url))
    url = File.join(url, index_file)
  end

  # Setup proxy
  if target = opts.delete(:proxy)
    # TODO: deprecate proxy through page?
    proxy(url, target, opts, &block) and return
  elsif opts.delete(:ignore)
    # TODO: deprecate ignore through page?
    ignore(url)
  end

  # Setup a metadata matcher for rendering those options
  sitemap. url do |url|
    { :options => opts, :blocks => blocks }
  end
end

#with_layout(layout_name, &block) ⇒ void

This method returns an undefined value.

Takes a block which allows many pages to have the same layout

with_layout :admin do
  page "/admin/"
  page "/admin/login.html"
end

Parameters:

  • layout_name (String, Symbol)


30
31
32
33
34
35
36
37
# File 'lib/middleman-core/core_extensions/routing.rb', line 30

def with_layout(layout_name, &block)
  old_layout = layout

  set :layout, layout_name
  instance_exec(&block) if block_given?
ensure
  set :layout, old_layout
end