Im trying to output front matter variables depending on the current Jekyll environment.
For example, if the current environment is dev I would like the variable dev to be displayed:
---
something:
dev: "Some text"
production: "Other text
---
I'm trying to access the variable dev with the following method and I get no result:
{{ page.something.jekyll.environment }}
Is there a better way to do this?
I don't think you can access front matter in this way.
Instead, you coud assign the dev variable in your html template, with the environment as a conditional:
---
layout: default
---
{% if jekyll.environment == "dev" %}
{% assign dev = "this is dev" %}
{% endif %}
<div id="page-content">
{{dev}}
...
</div>
You could also directly wrap the relevant element in an if statement in the template.
The right syntax is : {{ page.something[jekyll.environment] }}.
Note that locally jekyll.environment is "development" and not "dev".
Related
I'm trying to clone the output of index.html in another page, thanks.html. It's working, but no matter what I try I end up with --- layout: default title: example --- rendered (from index.html's Front Matter) where the file starts.
I've tried:
---
layout: default
thanks: true
---
{% capture main %}{% include_relative /index.html %}{% endcapture %}
{{ main }}
And:
---
layout: default
thanks: true
---
{% include_relative /index.html %} <!-- Same problem with just 'include' -->
But I get the same result.
If I drop the line layout: default it works, but as expected I don't have a header or footer anymore. I can also get it to work using includes but this makes for a lot of duplication for a page with a single line that's different.
In case it's relevant, I'm running Jekyll version 4.2.2 via docker, and there are no plugins set in my _config.yml.
A fragile hack:
{% capture source %}{% include_relative included.html %}{% endcapture %}
{{ source | split: "---" | last }}
Just don’t use --- anywhere in the included document. :)
I have this Liquid template which looks like this:
# /_includes/slideshow.html
{% for image in {{ include.images }} %}
...
{% endfor %}
which I'm trying to use with a YAML file (for my Jekyll site) like this:
# /index.md
{% include slideshow.html images='site.data.homepage_images' %}
The reason I see this failing is because my include variable {{ include.images }} resolves to a string within the for loop. Is there a different way to accomplish this? I'm still rather new to Liquid, YAML, Jekyll, and well, web development altogether, so any help in doing this is much appreciated!
(Note: the problem goes away if I replace {{ include.images }} with site.data.homepage_images.)
Additionally, the reason why I'm doing this (and why that crude fix isn't the solution I'm looking for) is for the ability to inject my image slideshow elsewhere around my site. It'd save a lot of code to abuse my include variable in this way.
Correct syntax in for loop is : {% for image in include.images %}
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...
Hi I'm working on a Jekyll project and I need to put a variable in _config.yml that I want to change dynamically from the template code.
This is what I want to do, but I can't get it to work. Is it possible to do this?
In _config.yml:
my_var: "value"
In template.html:
{% site.my_var = "newvalue" %}
{% case site.my_var %}
{% when "value" %}
//do this
{% when "newvalue" %}
//do this instead
{% endcase %}
While you apparently cannot use Liquid conditionals nor Environment Variables (as in many other build scripts), you can do a selective overrides with a second yml file:
$> bundle exec jekyll serve --drafts --incremental --config _config.yml,_dev.yml
with a _dev.yml:
# overrides title in _config.yml
title: "My Website (dev mode)"
# see my styles uncompressed for dev work
sass:
style: uncompressed
Perhaps that fits your needs...
New to Jekyll and wondering if it's possible to include custom variables in Jekyll Front Matter. It would be useful for nested layouts, for example something like:
layouts/artist.html
----
layout: default
title: {{ page.artist }} (Artist)
----
I get an error trying that.
At the moment, Jekyll do not support Liquid variables in the front matter, and the only way to do that would be through a plugin, such as jekyll-conrefifier.
Alternatively, what you can do, though, is create variables that you re-use on the same file:
{% assign new_title = page.title | append: " (Artist)" %}
<h1>{{ new_title }}</h1>
and you can also pass variables to files that get included. For example, including a file from _includes\display-post.html passing the modified title as an argument:
{% assign new_title = page.title | append: " (Artist)" %}
{% include display-post.html post_title=new_title %}
And then getting the value of the value passed in (example content of _includes\display-post.html):
{% assign title_received = include.post_title %}
<h1>Title that as passed in: {{ title_received }}</h1>
I'm not sure if there is a way to do this properly (i.e. server side), but a stop-gap measure could be to have a small snippet of Javascript that sets the correct title in the users browser. e.g.
---
title: Default title blah blah
---
[... content ...]
<span id="pagetitle" style="display: none">{{ page.artist | escape }} (Artist)</span>
<script type="text/javascript">
var pagetitle = document.getElementById("pagetitle");
if (pagetitle) {
document.title = pagetitle.textContent;
}
</script>
Notes:
The substitution of page.artist is performed in HTML rather than in the Javascript because it is easier to quote any HTML special characters (via escape) rather than the Javascript special characters ' or " or \ (there isn't a built-in filter to do this).
One could also move the pagetitle span to the top of the page so that it is near the other YAML front matter.
Unfortunately, this is a very poor way of achieving this, but it looks like it might be the only way other than writing a plugin.