Conditionally include content from collection on page

Hi all. I’m stuck on how to display a conditional statement from a collection page on a non-collection page. My file structure is a little complex, but necessary because of the way I’m implementing a multilingual website. Here is what I’ve got so far.

I am using a file in a collection to hold the content that will ultimately appear in one section of a page…

my_collection/about_section.md:

---
section_id: about_section
…
---

{% capture content_french %}
    Quelques paragraphes en français.
{% endcapture %}

{% capture content_english %}
    Some paragraphs in English.
{% endcapture %}

{% if include.language == "content_french" %}
  {{ content_french }}
{% endif %}

{% if include.language == "content_english" %}
  {{ content_english }}
{% endif %}

Then, I loop through this collection page in an _includes file that I am using to mark up the layout of the about.md file above, as well as other collection files. (I’m not sure if a parameter can be used in a variable, as shown below, but I don’t know how else to do it.)

_includes/section_layout.html:

{% for item in site.my_collection %}
    {% if item.section_id == "about_section" %}

        {% if page.lang == "fr" %}
            {{ item.content language="content_french" }}
        {% else %}
            {{ item.content language="content_english" }}
        {% endif %}

    {% endif %}
{% endfor %}

Finally, I have two user-visible pages (one for each language), which both pull in the same _includes file (and other includes).

fr/about/index.md:

---
lang: fr
---

{% include section_layout.html %}
…

en/about/index.md:

---
lang: en
---

{% include section_layout.html %}
…

As it is, {{ item.content }} is not showing up on either the French or English page. Some other content that I put in the collection’s front matter does show up, but I’m struggling to display {{ item.content }} area with these conditions. If a secondary _includes file is needed to pull in {{ item.content }} to achieve what I want, I am willing to do that.

Ideally, in about_section.md, instead of the captures, I would like to be able to write something like the following:

{% if page.lang == "fr" %}
    French content
{% else %}
    English content
{% endif %}

But that doesn’t work, presumably because page.lang refers to this collection page itself, rather than the page where I want the content to appear (e.g. fr/about/index.md).

Any ideas would be appreciated. Many thanks in advance.

includes can have parameters so this:

en/about/index.md:

lang: en

{% include section_layout.html %}

{% if page.my_variable %}
  {% include {{ page.my_variable }} %}
{% endif %}

maybe that will help? why not use the include straight off instead of using a collection? also I think you could use the front matter variable in the include file name.

Thanks for your help so far. If I understand correctly, that wouldn’t work for my case.

I am using a collection, instead of using the include straight off, because each collection page has a bunch of front matter containing content for both languages. For example, in my_collection/about_section.md:

heading:
  - lang: en
    text: English heading goes here

  - lang: fr
    text: Le titre français va ici

So, my hope is to keep all of the content for a given section, both English and French, in a single collection page. I’ve managed to set up include parameters to show the correct front matter from the collection page based on whether the page’s language is set to English or French. My problem is that there is also content (i.e. the body of the section) for both languages in the content area (as opposed to the front matter) in each individual collection page, and I can’t figure out how to show each language based on the language setting of the page where it will appear.

I’m sorry this is so complicated. I think it’s a fairly good approach to a multilingual site; I just can’t figure out how to deal with the content area (i.e. non-front matter area).

I think I am starting to understand what you want to do, but I don’t think you will be able to get the value of the Capture on the collection page into another page. A full actual file for one collection page would make it more clear. Also I could be wrong, sometimes people have creative solutions to things like this that are not obvious.

I could see how you could maybe do it if it was all in the front matter but that wouldn’t support much content.

I’ve not done a multilingual site before, but I think most people do a full page in both languages and then switch that based on the lang=. I’ve seen a few blog posts about how people do it, might be worth seeing what other ways people have found to do it.

Your way be ok for 2 languages (if it worked), but what if there were 4? would you have all 4 languages details in one file? that seems hard to maintain vs a separate file for each.

Yeah, I see what you mean. There are pros and cons to this approach. The big pro of having one file for all languages is that there would be a bunch of front matter that I wouldn’t have to repeat across files (e.g. image sources, link destinations, CSS classes for that section, etc.).

As you say, one con is that it might get a little unwieldy if I am dealing with many languages, but I don’t think that would get out of hand, because each collection file is only for one discrete section of a page (i.e. not the entire page). So each collection file would have only a few paragraphs (and maybe a list) in the content area.

I suppose one solution is to create a collection page for each language and put only the main “content” (i.e. body) for each language in each file, but keep all of the front matter for all languages in the English collection page (to avoid repeating). That’s not as ideal for me as having everything in one file, but if the content area can’t have those conditional statements in them, then oh well.

I appreciate you taking the time.