Call a specific page variable in Jekyll

时间:2015-08-15 07:24:16

标签: variables jekyll

Is it possible to call a specific page.variable into a layout in Jekyll?

I know it is possible to use {{ page.title }} in a layout to call the current page title, but is it possible to call another page's title instead.

e.g.

{{ pageX.title }} 

Would return the title from pageX.html

I have searched and couldn't see a solution, I just wanted to see if anyone knows if this is even possible.

3 个答案:

答案 0 :(得分:4)

您可以在site.pages中使用网页索引。例如:{{ site.pages[0].title }}

答案 1 :(得分:2)

这取决于您希望如何选择特定页面。例如,我有一个名为" unit" (可在" site.categories.units"下访问)。对于每个"单位"第一页定义了YAML varibable" unit-id"以独特(和人类可读)的方式。

想象一下,我想使用" unit-id"来访问该单元。 #&34;我的 - 第一单元&#34 ;.我可以使用以下方式访问此页面的内容:

{{ site.categories.units | where: "unit-id","my-first-unit" }}

如果我想要做更复杂的事情,我需要访问YAML前言中存储的任何信息(让我们称之为"无论是什么-YAML变量")页面,它涉及更多:

{% assign this-unit = "my-first-unit" %}
{% for unit in site.categories.units %}{% if unit.unit-id == this-unit %}{{ unit.whatever-YAML-variable }}{% break %}{% endif %}{% endfor %}

请注意,de loop中的指令是收缩的(没有换行符),以避免输出HTML中出现多个空行。此外,我添加了一个"休息"减少处理器资源浪费的指令。

另请注意,您可以轻松添加几行来控制" my-first-unit"页面存在,甚至在重复的情况下解决冲突。

我承认这是一种相当笨拙的方式,但它是第一个解决方案。我期待着任何其他更强有力的建议。

答案 2 :(得分:0)

假设您的文件名是唯一的,您可以通过过滤slug的所有页面来避免循环:

{% assign mypage = site.documents | where: "slug", "my-page-slug" | first %}
  {{mypage.title}}

如果您有一个名为“pageX.md”的页面,那么您的slug将是“pageX”。