Is it possible to offload frontmatter to an include in some manner? My Jekyll site uses a frontmatter variable for the page title, however I have some pages that share an include due to code repetition. Putting the frontmatter for the page title in the include treats it like raw HTML. Is there a way to simulate this and set variables in an include?
Asked
Active
Viewed 2,236 times
3 Answers
3
According to the Jekyll docs on passing parameter variables to includes, you need to capture your front-matter as a variable first.
Their example uses capture, but you can use assign if you're just passing the variable through. Here's my tested example of how to do this:
include
<div>
{{ include.title }}
</div>
include in content
{% assign include-title = page.title %}
{% include example.html title=include-title %}

Edward
- 1,399
- 11
- 23
1
There is no front matter in includes.
But you can reach page's title from inside an include by using page.title
, and any other page's variable with page.myVarName
.

David Jacquel
- 51,670
- 6
- 121
- 147
-
Apologies for confusion in the question. I know that frontmatter doesn't work in includes, and I know variables cannot be changed once they are set. Is there a workaround to declare the page title variable inside the include? Essentially, I want the page title to come from the data file that is used in the include. – RedBassett Jul 08 '15 at 20:38
-
So, you can edit your question and explain a little more. Give us some small code samples of what you've already tried. – David Jacquel Jul 08 '15 at 22:53
-1
You can pass a parameter to an include. Try:
{% include name.html var={{page.title}} %}
Then in the include, access the parameter using include.var
.
See "Pass Parameters to Includes" at https://jekyllrb.com/docs/includes/

Karan Gill
- 160
- 5
-
Passing parameters to includes is powerful and would likely work for what you're trying to do. On the includes page, see this section in particular: [Passing parameters to includes](https://jekyllrb.com/docs/includes/#passing-parameters-to-includes). – Tom Johnson Apr 02 '17 at 14:52
-
1this is incorrect and will result in an error. You must turn the front matter into a variable via the assign or capture method., then pass through to the include. I have detailed this method in my answer above: https://stackoverflow.com/a/55616695/3411192 – Edward Apr 10 '19 at 15:52