Search code examples
rubyjekyllliquidjekyll-theme

Pass site collections from page to layout jekyll


I'm trying to pass one site collection to my page's layout, in order to be able to make my nav menu according to the sections I pass.

In my _config.yml

collections:
  tt:
    output: true

In my index.md page:

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

In my layout:

---
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 is under _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)

In my layout, using {%- assign _sections = site.sec | flatify -%} works perfectly, but when I pass the collection from the page to the layout, it's not working.

If I do the exact same thing passing site.title instead of site.tt from the page to the layout, everything works just fine. But when I try to pass a collection, it's not working.

Thanks for your help.


Solution

  • Your flatify plugin is cool, but it does not reflect real life.

    You cannot use liquid vars in front matter because they are not parsed.

    In your page's front matter :

    ---
    sec: "tt"
    ---
    

    Then, from the page or the layout, you can just call :

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

    If you want to debug, you can use inspect filter, which just outputs variable content.

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