Can an external template loaded with the 'include' keyword contain Jinja syntax? - jinja2

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

Related

Prevent Liquid from parsing of certain files in Jekyll

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 %}

Include raw fragments of collection files with Jekyll

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.

Adding a variable in a Liquid template for multilingual?

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 %}

Is it possible in gh-pages Jekyll to have {{content}} expand without evaluating liquid tags?

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

How to highlight CSHTML in Jekyll/Pygments

I'm using Pygments for highlighting code snippets in Jekyll.
I want to know how to highlight .cshtml/.aspx files using Pygments highlighter
Here are the available lexers in Pygments. Since ASPX might contain C# or VB, it should be supported in Pygments since it is on the supported languages list.
The relevant ones that you are looking for would be aspx-cs and aspx-vb, as those highlight ASPX code snippets.
{% highlight aspx-cs %}
//your aspx code here
{% endhighlight %}
Or if those aspx code snippets mainly contain static html or xhtml, you should be able to just {% highlight html %}.
CSHTML should probably just use csharp or c# as the short name.
A readable reference list of languages and their shortnames are found here.