In a Jekyll layout, default.html, you initialize a variable
{% assign relative_path = "../../" %}
Inside a page using default layout, this variable is empty though.
How can you use inside pages variables set in layout?
This is the opposite of
Can you use Jekyll page variables in a layout?
After some research in Jekyll code, it seems that you cannot access layout variables from a page or post. The only information about layout a page can see is the layout's name {{ page.layout }}.
Then you can use this to store some layout related variables in _config.yml and get them from any post/page using the {{ page.layout }} variable.
_config.yml
layoutVars:
default:
relative_path: "../../"
post:
relative_path: "an other path"
page:
relative_path: "hello world"
use in a page or post
{% assign pagePath = site.layoutVars[page.layout].relative_path %}
You have two possibilities.
Store your variable in the layouts yaml-header, e.g.:
---
variable : value
---
Have a look at jekylls front matter defaults. You can declare variables depending on the type of posts/documents and the folder the posts are stored in.
I'm not if this is what you are looking for, but i hope it helps...
Related
I have a layout used for the home page for different sections on my Jekyll site. On each of these pages I would like to have links to each item in the section, the details of which are stored in a YAML file in the site _data directory. My aim is to have the name of the site data variable in the section page front matter and pass this into the layout for rendering. For example:
Page Front Matter
---
sectionItems: site.data.sectionItems.awesomeSectionItems
---
...which is passed to the section home layout...
Section Home Layout
{% for item in page.sectionItems %}
// Work with section item...
{% endfor %}
Unfortunately, when I run the site nothing appears. How would I go about doing this? I have also tried an include but this also does not work. I would like to avoid adding the for loop to each page, plus I would like the links to appear beneath the main content section.
You cannot use variables in front matter. You will have to use a content variable like {% assign sectionItems = site.data.sectionItems.awesomeSectionItems %} and then loop with {% for item in sectionItems %}.
Let's say I have a bunch of _data files that I use to create a list for specific pages. All of the pages that have these lists have a custom front matter variable of pageName. The value of pageName just so happens to match the _data file with the list.
Rather than pasting the html blocks of code into each page, I'd like to use an include to pull in a generic block of html. To do that, the include has to be dynamic or contain liquid markup to become dynamic.
That might be more context than you need, but my question boils down to this, is something like this possible:
{% for item in site.data.{{page.pageName}} %}
{{ item.label }}
{% endfor %}
Unless I'm missing something, this doesn't work so I'm wanting to know if there's a way of achieving similar functionality.
I found this similar question, but it doesn't seem to answer whether it can be used to access a page variable to build a file name.
You can use the bracket notation like this :
{% for item in site.data[page.pageName] %}
Consider the following three snippets from the specified files:
<!-- _includes/my_var.html -->
{% assign my_var = 'foo' %}
and
<!-- _layouts/default.html -->
{% include my_var.html %}
...
and a post:
---
layout: default
---
I'd like to reference {{my_var}}
The last one doesn't appear to see the variable defined in the includes file. I get nil as the value of my_var. Am I wrong to expect that it should be in scope? If so, what's an alternative approach?
You cannot get layout variables from page or post.
But, as it looks like a configuration variable, you can set it in _config.yml and access it from anywhere with site.my_var.
You can do that with frontmatter as well.
---
layout: default
myvar: foo
---
I'd like to reference {{page.myvar}} OR
I'd like to reference {{post.myvar}}
I need to be able to exclude certain files from the build. I am aware I can do this in the config file.
I also need a way to turn off a section of the website in the nav.
So I thought about having a flag in a data file, if it's false do not include a link to the section in the nav.
But how can I also use the same flag to prevent the section from being built?
Or is it easier to specify in config and check this flag in the nav?
To exclude files from builds, add this line to your _config.yml:
keep_files: [folder, "file.ext"]
The folder and the file.ext will be left untouched by Jekyll and will be included on builds.
OR
exclude: ["file.md", "otherfile.html"]
Both files won't be included on you site built by Jekyll at all.
Here:
I also need a way to turn off a section of the website in the nav.
I'm not sure what you meant, but I guess you can probably do that with if statements:
In a post or a page front matter, add a variable indicating the section to exclude:
---
# your front matter settings
foo: bar # variable and value
---
Then, to your template, add:
{% if page.foo %}
<div>this will display</div>
{% endif %}
or
{% if page.foo %}
<div>this will display</div>
{% else %}
<p>this will display when the above doesn't</p>
{% endif %}
Hope to have helped! :)
Seems like a config problem to me. I would put it in the config and have your includes and nav check the config values.
I need to provide page content reference list (it should contain references on sections on page).
The only way which I can see is to use page.content and parse it, but I stumbled on problem with data evaluation. For example I can pull out this string from page.content: {{site.data.sdk.language}} SDK but there is no way to make jekyll process it, it outputs as is.
Also I want to make it possible to create cross-pages links (on specific section on page, but that link generated by another inclusion and doesn't persist in page.content in HTML form).
Is there any way to make it evaluate values from page.content?
P.S. I'm including piece of code which should build page content and return HTML with list (so there is no recursions).
P.P.S. I can't use submodules, because I need to run this pages on github pages.
Thanks.
Shouldn't {{ site.data.sdk.language | strip_html }} do it? I don't know, most probably I didn't understand the problem. Can you elaborate more? Maybe provide a link to a post you're referring to?
Thinking about the similar
{% assign title = site.data.sdk.language %}
which is a stock Liquid tag and does the job well, so instead of
{% section title={{site.data.sdk.language}} %}
write your code as
{% section title = site.data.sdk.language %}
The key here is that once you enter {%, you're in Liquid. Don't expect Liquid to go "Inception" all over itself. {{ is just a shorthand for "print this to output", but an parameter passing is not output, it's just reading a variable.
You should be able to also go wild and do:
{% section title = site.data.sdk.language | capitalize %}
For more re-read the docs: https://github.com/Shopify/liquid/wiki/Liquid-for-Designers