3
votes

I am trying to set a Jekyll (Liquid) variable in my content page, and exposing that variable and its content in the including Jekyll template. In this use case, I want to author the main page content, and side-bar content in a single MD file, but control the injection of said content.

Here is my setup..

_layouts/default.html

<div>{{ content }}</div>
<div class="side-bar">{% include sidebar.html sidebarContent=sidebar %}

_includes/sidebar.html

{{ include.sidebarContent }}

pages/my-content-page.md

----
layout:default
----

This is the main content portion's content.

{% capture sidebar %}
This is the sidebar content

* one
* two
{% endcapture %}

It seems like there is no way to pass the "sidebar" variable UP the scope; I can only seem to pass it down. All i want to to do is separate the content authoring for a single page (which consists of 2 discrete areas; Content and Sidebar) and avoid introducing layout marking into my "content" files (aka the MD files).

If there is a way i can break them out into different MD files i'db be open to that as well... ex.

my-page.md
my_page_sidebar.md

or

my_page/index.md
my_page/sidebar.md

I have some leeway in this as im building out using pages not posts.

1

1 Answers

4
votes

From a page, you can get datas from front matter and content itself when you are in a layout. So only page.something variables are available.

But, there is no way to capture or assign in page content and get the resulting variable from a layout.

You can try to store your sidebar in your front matter, but it can be tricky.

One solution can be to separate content from sidebar with something like an excerpt separator used by posts.

In _config.yml, set

content_separator: "<!--separator-->"

In your page :

This is the main content portion's content.

{{ site.content_separator }}

This is the sidebar content

* one
* two

In your template :

{% assign contentSplitted = content | split: site.content_separator %}
{% assign myContent = contentSplitted | first %}
{% assign navbar = contentSplitted | last %}

<div>{{ myContent }}</div>
<div class="side-bar">{{ navbar }}</div>

Note I use myContent as content variable is already in use.