Jekyll Liquid - accessing _config.yml dynamically
With the way your vars are set, it would be something alog the lines of
{{ site.locales[site.default_locale][page.title] }}
The thing is, ... I don't really see the point of doing this. Let's say your page is the english page. The locale should then be defined within the page, and so should your title!
---
locale: en
title: My Wonderful Page
---
Which you can retrieve with {{ page.title }}
...
What could be the point of putting the title into the _config.yml
file?
(edit) well unless you want to access page.title
when in another page/post, in this case you have no choice but to put it into _config.yml
.
Dynamic Variables Jekyll Liquid
The title that you want to pull is form the site config. Not the page itself. All you need to do is change the call in your Default Layout listing to this:
{{ site.locales[site.default_locale].title_homepage }}
When you set default_locale: "en"
the output will be "This is my homepage title!". When you update the _config.yml file to default_locale: "pirate"
, the output will be "Yaaawwwr. Homepage title." I've tested this on Jekyll 0.11.2 and it works as expected.
How can I create a Jekyll variable from another in _config.yml?
My advice would be to create two variables in your config.yml, like this:
url: https://www.example.com/
baseurl: site/
imageurl: images/
And use them in your template, like this:
{{ imageurl | prepend: site.baseurl | prepend: site.url }}
Access variables defined in _config.yml within Jekyll Hook
Aha, one has to pass the site variable into the hook itself:
Jekyll::Hooks.register :site, :post_write do |site|
if site.config['create_index']
system('npm run index')
end
end
How to dynamically create a navigation based on Front Matter for Jekyll?
Simple way to dynamically manage documentation through front matter without the need for a pesky config file.
Three variables one can set in front matter:
parent
: Represents if this should be nested within another page for navigationhidden
:false
for a page to be publicly listed, it'll be accessible via direct link regardlessnav_priority
: Weights which document to show first, 1 is highest (ie top)
Logic
- Can default to hide/show everything within
_config.yml
- A page shows up as a header if it:
- has no parent OR
- is a parent page
- If a page has a parent, it will show up underneath the parent
- Can only have 2 levels
- If a parent is hidden, then all child pages are also hidden
- Weighting can be defaulted for documents in
_config.yml
to100
to keep them at the bottom, or1
for at top
the below assumes the files are in _docs
if elsewhere change site.docs
accordingly
{% assign pages = site.docs | where: 'hidden', false %}
{% if pages.size > 0 %}
{% assign head_pages = pages | where: 'parent', nil | sort: 'nav_priority' %}
{% for head_page in head_pages %}
<h5>
<a href="{{ head_page.url }}">{{ head_page.title }}</a>
</h5>
<ul>
{% assign child_pages = pages | where: 'parent', head_page.title | sort: 'nav_priority' %}
{% for child in child_pages %}
<li>
<a href="{{ child.url }}">{{ child.title }}</a>
</li>
{% endfor %}
</ul>
{% endfor %}
{% endif %}
Include jekyll / liquid template data in a YAML variable?
I don't believe it's possible to nest liquid variables inside YAML. At least, I haven't figure out how to do it.
One approach that will work is to use a Liquid's replace filter. Specifically, define a string that you want to use for the variable replacement (e.g. !SITE_URL!
). Then, use the replace filter to switch that to your desired Jekyll variable (e.g. site.url
) during the output. Here's a cut down .md file that behaves as expected on my jekyll 0.11 install:
---
layout: post
excerpt: In the [earlier post in this series](!SITE_URL!/2013/01/12/)
---
{{ page.excerpt | replace: '!SITE_URL!', site.url }}
Testing that on my machine, the URL is inserted properly and then translated from markdown into an HTML link as expected. If you have more than one item to replace, you can string multiple replace calls together.
---
layout: post
my_name: Alan W. Smith
multi_replace_test: 'Name: !PAGE_MY_NAME! - Site: [!SITE_URL!](!SITE_URL!)'
---
{{ page.multi_replace_test | replace: '!SITE_URL!', site.url | replace: '!PAGE_MY_NAME!', page.my_name }}
An important note is that you must explicitly set the site.url value. You don't get that for free with Jekyll. You can either set it in your _config.yml
file with:
url: http://alanwsmith.com
Or, define it when you call jekyll:
jekyll --url http://alanwsmith.com
Set the data file to use in Jekyll dynamically
You are almos there, surround the test file name with quotes and access the data array directly with site.data[data_file]
:
<ul>
{% assign data_file = "testFile" %}
{% for item in site.data[data_file] %}
<li>{{ item.nm }}</li>
{% endfor %}
</ul>
How to access object from _config.yml with reference from post in Jekyll?
Use page.author
, not post.author
:
<a href="">{{ site.users[page.author].name }}</a>
Your Yaml should look something like this:
users:
user_x:
url: "/some-url"
name: "Full Name"
user_y:
url: "/some-other-url"
name: "A Different Name"
You might want to use assign
if you’re using several values from the user:
{% assign user = site.users[page.author] %}
<a href="{{ user.url }}">{{ user.name }}</a>
Related Topics
Getting a Rogue Iteration from My .Each Loop
Instance_Eval Does Not Work with Do/End Block, Only with {}-Blocks
Can't Dup Nilclass on Association Methods
Bootstrap Modal in Ruby on Rails Not Working
Ruby Parenthesis Syntax Exception with I++ ++I
Using Poltergeist with a Proxy
How to 'Join' an Array Adding to the Beginning of the Resulting String the First Character to Join
Ruby Facebook Graph API Appsecret_Proof
How to Understand the #Dup and #Clone Operate on Objects Which Referencing Other Objects
Rails 404 Error for Stylesheet or JavaScript Files
Calling Instance Variables Without @
How to Get the Nth Element of an Enumerable in Ruby
Why Does Single '=' Work in 'If' Statement
Rails 3: Belongs_To, Has_One and Migrations
New Way of Creating Hashes in Ruby 2.2.0
Bundler: Not Executable: Script/Delayed_Job
Recursive Rails Nested Resources
Ruby CSV.Each in While Loop Not Executing Second Time Through