16

Is it possible to retrieve the current path of a page in a middleman file? For instance, if I have a layout file layout.erb with something like the following:

<%= page.path %>
<%= yield %>

and a test file index.html:

Testing

then when Middleman rendered the page I would get something like:

/index.html
Testing
LandonSchropp
  • 10,084
  • 22
  • 86
  • 149

2 Answers2

42

Middleman also provides the current_page variable. current_page.path is the source path of this resource (relative to the source directory, without template extensions) and current_page.url is the path without the directory index (so foo/index.html becomes just foo).

<%= current_page.path %>
# -> index.html

<%= current_page.url %>
# -> /

Details from Middleman's Middleman::Sitemap::Resource rubydoc. http://rubydoc.info/github/middleman/middleman/Middleman/Sitemap/Resource

Nick
  • 858
  • 8
  • 9
2

The solution is:

<%= request.path %>
LandonSchropp
  • 10,084
  • 22
  • 86
  • 149
  • If I had to guess, I would say when Middleman builds the application it creates a server, requests the pageas in the application and writes the responses to files. But I honestly don't know. – LandonSchropp Apr 18 '13 at 05:52
  • @jackyalcine it is aliased to the resource object for the current path https://github.com/middleman/middleman/blob/dd0ee24ca3ae728b00f8987d74c360557c910946/middleman-core/lib/middleman-core/template_context.rb#L165 – David Silva Smith Dec 19 '15 at 20:22