Mr Teal
Mr Teal

Reputation: 342

How do I use a variable inside an include statement in a Jekyll file?

I have a multilingual Jekyll website and I'm trying to build a page that uses an include that will append a file based on the language used but I'd like to avoid using an if statement.

I tried to use the following options without success:

{%- assign filename = "myFile." -%}
{%- assign lang = site.active_lang -%}
{%- assign fileExtension = ".html" -%}
{%- assign file = filename | append: lang | append: fileExtension  -%}
{%- include file -%}

I was expecting the include to include run like this:

{%- include myfile.en.html -%}

But the include is adding a string into the page instead of the desired file.

Does anyone know if this is possible or I'll have to give up and use an if statement that is proven to work?

Upvotes: 2

Views: 412

Answers (1)

Mr Teal
Mr Teal

Reputation: 342

Solved using {{}} around the variable, like this:

{%- include {{file}} -%}

Upvotes: 1

Related Questions