I have a Jekyll project and I'm trying to implement multilingual functionality.
My data files were stored in _data/my_data.yml, and accessed in the templates via the {% for data in site.data.my_data %} Liquid template tag.
I have now copied and translated my data files into _data/en/my_data.yml and _data/it/my_data.yml and created two .md files, using the same template, with respective lang attributes in the front matter.
I am trying to dynamically access the correct data based on this lang attribute however it's throwing all sorts of errors.
Things I've tried
{% for data in site.data.[page.lang].my_data %}
{% for data in site.data[page.lang]my_data %}
{% for data in site.data.{{page.lang}}.my_data %}
Any ideas?
In case anyone runs into this in the future, solved it with pretty much the only combination I hadn't tried yet:
{% for data in site.data[page.lang].my_data %}
Related
I just learned about Jinja's include function and wondered if it can also evaluate Jinja syntax within the loaded template?
So what I mean is something like:
view1.html
{% block content %}
{% include "subview1.html" %}
{% endblock %}
subview1.html
{% for image in images %}
....
{% endfor %}
The include documentation states:
Included templates have access to the variables of the active context by default.
And reading the Import Context Behavior information, I learned that
By default, included templates are passed the current context, and imported templates are not.
So I conclude that the loaded templates can refer to the context of the main HTML. But it's not clear to me if they can also contain Jinja logic. Although I suspect it won't work since the templates are rendered first and then included.
The include tag is useful to include a template and return the rendered contents of that file into the current namespace
I am making a website using go templates. I need to print a variable containing JSON data to the screen. I'd like to pretty print it to the screen and haven't had any luck with that.
In flask I am able to do the following to accomplish this decently well:
{% autoescape false %} {{stringwithjsondata | safe | replace(",", "<br>")}} {% endautoescape %}
and was wondering if I could do something similar here.
I'm using the html/template package for the website.
EDIT: I over complicated this to eternity, I just used <pre> tags in the end with strings.replace to make it look good
I am using Jekyll + Liquid + Markdown to generate static html pages. Consequently, this is really a question specific to the Jekyll framework and Liquid template generator because there is syntax that conflicts with Liquid in some of my Markdown files.
Is there a quick and dirty work-around I can use for the time being in order to prevent Liquid from parsing certain files?
Use Raw:
Raw temporarily disables tag processing. This is useful for generating content (eg, Mustache, Handlebars) which uses conflicting syntax.
{% raw %}
In Handlebars, {{ this }} will be HTML-escaped, but
{{{ that }}} will not.
{% endraw %}
I need to include raw text from a bunch of files inside a Jekyll collection. I don't want to process them in any way, I just need to dump the raw text using a collection. It feels like the following should work:
---
layout: default
---
{% for item in site.fragments %}
{{ item.content }}
{% endfor %}
I have declared the collection in the _config.yml file, like so:
collections:
- fragments
Surprisingly, the output of item.content is not the raw text inside the file, but rather the processed HTML that results from running Jekyll on top of the file.
Clearly I must be misunderstanding something fundamental, since stitching together text files should be trivial for a text processor like Jekyll.
I'm trying to use GitHub Pages for my project's documentation, but it includes generated html files that turn out to have illegal liquid tags. I don't need any expansion beyond the _layout itself, but as far as I can tell, any {% ... %} tags in the articles' content themselves are also evaluated and there seems to be no way to suppress this, other than adding {% raw %}...{% endraw %} around the entire contents of every single article.
Is there any way to do this at the call site? Something along the lines of {{ content | unrendered }} would be excellent.
Note: this seems to be the opposite problem to many others, who are using page.content in a pre-render context and wanting it to be rendered; I've tried page.content but as far as I can tell it's exactly the same in my case, so no dice.
page.content was raw in the jekyll 2.x era. Now its rendered content.
You can use a hook plugin to add a page.raw field on any page.
Jekyll::Hooks.register :pages, :pre_render do |document|
document.data['raw'] = document.content
end
If you want to do the same on posts and collections items, use a documents hook :
Jekyll::Hooks.register :documents, :pre_render do |document|
Note :
In :pre_render hooks document.content contains raw content
In :post_render hooks document.content contains rendered content