中间人父母,兄弟姐妹,儿童方法

时间:2013-03-10 17:32:51

标签: ruby middleman

在中间人中创建页面时,如何指出哪些页面是父母/兄弟/孩子?该文档给出了一些指示如何使用父兄弟和子方法来构建导航和面包屑,但它没有说明如何安排目录中的页面,以便他们响应这些方法(父,兄弟,孩子)适当的方式。

Each resource in the sitemap is a Resource object. Pages can tell you all kinds of interesting things about themselves. You can access frontmatter data, file extension, source and output paths, a linkable url, its mime type, etc. Some of the properties of the Page are mostly useful for Middleman's rendering internals, but you could imagine filtering pages on file extension to find all .html files, for example.

Each page can also find other pages related to it in the site hierarchy. The parent, siblings, and children methods are particularly useful in building navigation menus and breadcrumbs.

这是父方法 http://rubydoc.info/github/middleman/middleman/Middleman/Sitemap/Extensions/Traversal#parent-instance_method

这是儿童方法 http://rubydoc.info/github/middleman/middleman/Middleman/Sitemap/Extensions/Traversal#children-instance_method

这是兄弟姐妹方法

http://rubydoc.info/github/middleman/middleman/Middleman/Sitemap/Extensions/Traversal#siblings-instance_method

1 个答案:

答案 0 :(得分:8)

在对Middleman的代码进行了一些探讨之后,我发现了一个Cucumber测试,它描述了#parent#children#siblings方法。

middleman / middleman-core / features / sitemap_traversal.feature

Feature: Step through sitemap as a tree

  Scenario: Root
    Given the Server is running at "traversal-app"
    When I go to "/index.html"
    Then I should see "Path: index.html"
    Then I should not see "Parent: index.html"
    Then I should see "Child: sub/index.html"
    Then I should see "Child: root.html"
    Then I should not see "Child: proxied.html"

...continued... 
  • 简而言之,似乎可以使用文件名“index.html”找到父级资源,因此如果您正在查看/foo/bar/some_resource.html,则可以在{{1}找到其父级。 }。
  • 它的兄弟姐妹与请求处于同一级别(注意“同名目录”被转换为'索引文件',例如。/foo/index.html变为/foo/bar/,使其兄弟姐妹在{ {1}}级别。)
  • 它的孩子处于以下水平。

通过将任何文件放在文件层次结构中的各自位置,您应该能够通过调用/foo/bar.html/foo/*或{{来引用该文件或包含该文件的集合。 1}}。

在阅读测试时,请注意已设置了几条“假”路线(#parent#children以及#siblings/sub/fake.html){ {3}}


更深入的潜水

如果你不能以面值进行黄瓜测试(我不怪你),还有更多!毕竟,这是什么,“我应该看到”和“我不应该看到”废话?嗯,有一个答案。

在测试的灯具中(in the config file),middleman/middleman-core/fixtures/traversal-app/是唯一包含任何内容的文件。在其中,您可以看到Child,Parent和Sibling路径打印在html文档的正文中。

layout.erb

fake2.html

这有助于解释测试,这些测试只是在响应主体中查找源自布局的字符串。您可以在Cucumber步骤定义(middleman / middleman-core / fixtures / traversal-app / source / layout.erb)中看到测试的确切行为。

最后,布局使用我们首先要描述的方法,/directory-indexed/fake.htmlfake2.htmlPath: <%= current_page.path %> Source: <%= current_page.source_file.sub(root + "/", "") %> <% if current_page.parent %> Parent: <%= current_page.parent.path %> <% end %> ...continued... ,它们是在中间核心中定义的。

middleman / middleman-core / lib / middleman-core / step_definitions /

#parent

我将不再解释Ruby代码,因为我必须快点上班。如果您希望我进一步调查,请在评论中告诉我,我会回复它。