【问题标题】:Pass site collections from page to layout jekyll将网站集从页面传递到布局 jekyll
【发布时间】:2020-04-24 06:34:59
【问题描述】:

我正在尝试将一个网站集传递给我的页面布局,以便能够根据我传递的部分制作我的导航菜单。

在我的 _config.yml 中

collections:
  tt:
    output: true

在我的 index.md 页面中:

---
layout: mylayout
title: My Great Homepage
icon: fa-home
order: 1
sec: "{{site.tt}}"
---

在我的布局中:

---
layout: mylayout
---
{%- assign _sections = page.sec | flatify -%}
{%- include header.html scrolly_nav=_sections -%}

<!-- Main -->
<div id="main">
    {{page.sec | flatify}} <!-- just to debug -->
</div>

Flatify 在 _plugins/flatify.rb 下:

module Jekyll
    module ExpandNestedVariableFilter
      def flatify(input)
        Liquid::Template.parse(input).render(@context)
      end
    end
  end

  Liquid::Template.register_filter(Jekyll::ExpandNestedVariableFilter)

在我的布局中,使用{%- assign _sections = site.sec | flatify -%} 可以完美运行,但是当我将集合从页面传递到布局时,它不起作用。

如果我做同样的事情,将 site.title 而不是 site.tt 从页面传递到布局,一切正常。但是当我尝试传递一个集合时,它不起作用。

感谢您的帮助。

【问题讨论】:

  • “将集合从页面传递到布局”是什么意思?
  • 前端不支持变量
  • @DavidJacquel 我的意思是,我不想在我的布局中使用 site.tt ,而是想让它变得通用,并能够在页面的前端变量中发送 site.tt 以在我的布局中使用页面变量。
  • @JoostS 这可以解释为什么我失败了。所以我需要每个集合一个布局。

标签: ruby jekyll liquid jekyll-theme


【解决方案1】:

你的 flatify 插件很酷,但它并不能反映现实生活。

你不能在前面使用液体变量,因为它们没有被解析。

在你页面的前面:

---
sec: "tt"
---

然后,从页面或布局中,您可以调用:

{%- assign _sections = site[page.sec] -%}
{%- include header.html scrolly_nav=_sections -%}

如果要调试,可以使用inspect过滤器,它只是输出变量内容。

{{ page.sec | inspect }} or {{ site[page.sec] | inspect }}

【讨论】:

  • 谢谢!完美运行,没想到网站是键值。
猜你喜欢
  • 1970-01-01
  • 2012-05-20
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
  • 2020-07-16
  • 1970-01-01
  • 2021-03-19
相关资源
最近更新 更多