yaml - 如何在 Jekyll/Liquid 中获取哈希的键和值?

标签 yaml jekyll liquid

我有一个 Jekyll 页面,设置如下:

---
navigation:
 - FOO: foo
 - BAR: bar
 - BAZ: baz
---

<h3>
  <a name="{{ page.navigation[0][1] }}" class="anchor" 
     href="#{{ page.navigation[0][1] }}">
  </a>{{ page.navigation[0][0] }}
</h3>

<p>Lorem ipsum dolor sit amet,.</p>

...

<h3>
  <a name="{{ page.navigation[1][1] }}" class="anchor" 
     href="#{{ page.navigation[1][1] }}">
  </a>{{ page.navigation[1][0] }}
</h3>

<p>Different content ...</p>

...

<h3>
  <a name="{{ page.navigation[2][1] }}" class="anchor" 
     href="#{{ page.navigation[2][1] }}">
  </a>{{ page.navigation[2][0] }}
</h3>

<p>Another different content ...</p>

...

我正在尝试使用 keyvalue我前面的每个导航项都与 href 有关上面例子中的链接。

我无法使用 {% for %}迭代,因为每个段落里面都有不同的内容。

我的愿望是让 Jekyll 输出以下代码:
<h3>
  <a name="foo" class="anchor" href="#foo">
  </a>FOO
</h3>

<p>Lorem ipsum dolor sit amet,.</p>

...

<h3>
  <a name="bar" class="anchor" href="#bar">
  </a>BAR
</h3>

<p>Different content ...</p>

...

<h3>
  <a name="baz" class="anchor" href="#baz">
  </a>BAZ
</h3>

<p>Another different content</p>

...

任何想法如何做到这一点?

最佳答案

您可以像这样围绕导航中的每个项目扭曲一个 for 循环:

<h3>
    {% for item in page.navigation[0] %}
     <a name="{{ item[1] }}" class="anchor" 
        href="#{{ item[1] }}">
     </a>{{ item[0] }}
     {% endfor %}
</h3>

<p>Lorem ipsum dolor sit amet,.</p>

<h3>
    {% for item in page.navigation[1] %}
     <a name="{{ item[1] }}" class="anchor" 
        href="#{{ item[1] }}">
     </a>{{ item[0] }}
     {% endfor %}
    </h3>

<p>Different content ...</p>


<h3>
    {% for item in page.navigation[2] %}
     <a name="{{ item[1] }}" class="anchor" 
        href="#{{ item[1] }}">
     </a>{{ item[0] }}
     {% endfor %}
    </h3>

<p>Another different content</p>

关于yaml - 如何在 Jekyll/Liquid 中获取哈希的键和值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25654209/

相关文章:

python - 使 pandas 系列安全的 YAML

ruby - Jekyll 的 pretty-print html 输出

jekyll - 我可以访问 Jekyll 布局中的页面目录吗?

javascript - SHOPIFY:如何根据特定变体显示 div

shopify - 如何在 Shopify 中的单个元对象页面上获取元对象值

GO 读取 YAML 文件并映射到结构 slice

azure - 如何避免在环境中的 Azure Agent for Pipeline 中创建新文件夹

symfony - 如何配置 Doctrine 以在 Symfony 4 上使用 yaml 映射

jekyll - 如何阻止 "jekyll build"覆盖输出目录中的现有文件?

Jekyll 博客不更新/显示新帖子