2016-02-16 54 views
1

我想在我的內容頁面中設置一個Jekyll(Liquid)變量,並在包含Jekyll模板中暴露該變量及其內容。在這個用例中,我想將主頁面內容和邊欄內容創作在單個MD文件中,但控制所述內容的注入。傳遞Jekyll變量「向上」範圍

這裏是我的設置..

_layouts/default.html中

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

_includes/sidebar.html

{{ include.sidebarContent }} 

頁/我的內容頁.md

---- 
layout:default 
---- 

This is the main content portion's content. 

{% capture sidebar %} 
This is the sidebar content 

* one 
* two 
{% endcapture %} 

看起來似乎沒有辦法將「側欄」變量傳遞給範圍;我似乎只能把它傳下去。我想要做的是將單個頁面(包含2個離散區域;內容和邊欄)的內容創作分開,並避免在我的「內容」文件(也就是MD文件)中引入佈局標記。

如果有辦法,我可以將它們分解成不同的MD文件我也可以打開它,以及...前。

my-page.md 
my_page_sidebar.md 

my_page/index.md 
my_page/sidebar.md 

我在使用網頁沒有這個職位作爲即時通訊建設了一些餘地。

回答

1

從頁面中,您可以從front mattercontent本身獲取數據,當您在佈局中時。所以只有page.something變量可用。

但是,在頁面內容中無法通過captureassign從佈局中獲取結果變量。

您可以嘗試將邊欄存儲在前臺,但這可能會非常棘手。

一個解決方案可以是將內容從側邊欄中分離出來,就像帖子使用的excerpt separator一樣。

_config.yml,設置

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

在你的頁面:

This is the main content portion's content. 

{{ site.content_separator }} 

This is the sidebar content 

* one 
* two 

在模板:

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

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

注意我用myContentcontent變量已在使用。

+0

哇!我喜歡這個想法@DavidJacquel!非常鼓舞人心! :) –

+1

很酷。但我認爲你可以用其他方式將內容從導航中分離出來。爲什麼,確切地說,你是混合內容和導航?這是一個頁面導航? –