untra/polyglot

site.static_files gets overridden (bug?)

dimpram opened this issue · 4 comments

Hello,

First of all, what a great plugin! It seems to be very easy to use. Great work! So I have a site that i am building and i used a kind of hacky way to implement image galleries similar to jekyll documentation below:

{% assign image_files = site.static_files | where: "image", true %}
{% for myimage in image_files %}
  {{ myimage.path }}
{% endfor %}

My site requires 2 languages: English and Greek. What i noticed is that when i go to the /el/ version the variable site.static_files doesn't actually point to the root of the project and thus i can't acces my images for my gallery unless i remove the "assets" from the exclude_from_localization: ["Gemfile", "Gemfile.lock", "assets", "_sass", "README.md"] something that i definitely have to avoid.

What i am realizing right now is that the way this plugin is implemented, this might be a feature and not a bug. Regardless, is there any way i can work around this? Am i missing something?

EDIT: Added link to my code

untra commented

Hi @dimpram 👋

I actually did not know about that site.static_files liquid variable, but from what I can read in the jekyll docs:

A static file is a file that does not contain any front matter. These include images, PDFs, and other un-rendered content. They’re accessible in Liquid via site.static_files.

huh, i didn't know that was available. polyglot could probably overload that for the other languages to make things more accesible when listing static and exclude_from_localization files.

Nonetheless the answer to your problem is more liquid syntax!

What i noticed is that when i go to the /el/ version the variable site.static_files doesn't actually point to the root of the project

so instead you might want to try something like this:

{% assign image_files = site.static_files | where: "image", true %}
{% for myimage in image_files %}
  {% if site.active_lang == site.default_lang %}{{ myimage.path }}{% else %}{{site.active_lang }}/{{ myimage.path }}{% endif %}
{% endfor %}

hopefully this helps!

-Sam

Thanks for the quick reply,

This solution doesn't work because the problem begins at the {{ site.static_files }} variable which accesses (or points to) different files for each page. I am going to try to make this clearer by providing an example.

Let's say i have a page called gallery.html which uses the previously mentioned code to display images - even if i just print {{ site.static_files }} in that page i get the artifacts that correspond to the static files in the project. If now i try to access the same gallery.html via el/gallery.html, when i print the variable {{ site.static_files }} i get no image artifacts at all.

In fact what i noticed is that {{ site.static_files }} provides access to the static files only inside of the el/ folder that gets generated inside the _site/ folder.

So assuming the example mentioned and exclude_from_localization: ["assets", "_sass", "README.md"] exists in the config file, the static files that i have access to at el/gallery.html are Gemfile, Gemfile.lock and favico.

If i remove the "assets" variable from exclude_from_localization: ["assets", "_sass", "README.md"] i can access the images from el/gallery.html because then those assets are copied over to the el/ folder and thus {{ site.static_files }} has access to them.

untra commented

In fact what i noticed is that {{ site.static_files }} provides access to the static files only inside of the el/ folder that gets generated inside the _site/ folder.

Okay, this behavior makes sense in context of what polyglot does. Yeah, the availability of static files is totally changed in sub-language pages, and excluded files wont be available locally during those site build. site.static_files is not going to behave correctly in sub-language projects, thats defs a bug.

For now, a workaround is (still) more liquid syntax. You (should) know the filenames of the relevant images for your gallery, so you can specify those files in your _data directory and then iterate over those files, using the filenames to create the correct paths to <img src="..."> tags that can point directly into your top-level assets directory. Thats how I would like to a consistent set of images for a gallery on multiple sub-language sites.

Hopefully this helps? I'll look into this bug, thanks for bringing it to my attention!

untra commented

Heya @dimpram ,

I was looking into this right now, and yeah its kindof a pickle.

The static_files do get built in jekyll gradually as it looks through files in the build directory. And given the point of the exclude_from_localization files are supposed to files you want in your main site and not in your sublanguage sites, I actually think this is working as intended. I can see use cases for not wanting to change this variable for sublanguage sites. (and to change it otherwise pulls a big thread in changing standard jekyll behavior). I can also see your use case as well; having direct access to the static_files paths in the default site. I'm not sure how to cleanly implement this, however.

So assuming the example mentioned and exclude_from_localization: ["assets", "_sass", "README.md"] exists in the config file, the static files that i have access to at el/gallery.html are Gemfile, Gemfile.lock and favicon

One suggestion I have here: use the jekyll exclude: ['Gemfile.lock', 'Gemfile'] variable to prevent those rubygem files from being available in your built jekyll site entirely.

I would still advise directly linking to file paths you have rather than iterating through the files in a directory at build time. Not this release.