6
votes

I would like to use dynamic variables for my jekyll + liquid installation. I want to dynamically access the _config.yml file using the dynamic variable names.

It is best explained with an example:

Page:

---
layout: default
title: title_homepage
---

Default Layout:

{{ site.locales[site.default_locale].page.title }}

_config.yml:

default_locale: "en"

locales:
  en:
    title_homepage: "This is my homepage title!"
  pirate:
    title_homepage: "Yaaawwwr. Homepage title."

So how can I access the _config.yml with a dynamic variable name?

2
To make sure I'm clear. You're '_config.yml' file will always be named exactly that. To make the change you'll update the 'default_locale' variable inside '_config.yml' from 'en' to 'pirate'. At that point, when you run jekyll, you want the output in your default layout to show "Yaaawwwr. Homepage title." instead of "This is my homepage title!". Is that accurate?Alan W. Smith
Hi Alan, yep. that is correct. Then I'll generate one _site for en, another one for _pirateHendrik

2 Answers

3
votes

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.

0
votes

Jekyll is a static website generator, its unable to usa a variable. But I guess for locale it self, there are one simple solution.

Follow this file structure (or something like that):

root
  - _include
    home.html
  - _layout
    default.html
  - en
    index.html
  - pirate
    index.html
  index.html

That simple, now use include for {{content}} and create simple files with variables and one single line {% include index.html %}

_include/index.html :

<h1>{{page.title}}</h1>

en/index.html :

---
layout: default
title: "This is my homepage title!"
---
{% include home.html %}

pirate/index.html :

---
layout: default
title: "Yaaawwwr. Homepage title."
---
{% include home.html %}

So... that is it.

Now create a page variable to handle locale urls for each page....

I hope this help.