Using a variable with Jekyll's contain operator - jekyll

Is there any way to do something like this?
{% if file.basename contains {{basename}} %}
All the examples I have seen explicitly use a value, like this:
{% if file.basename contains 'image' %}
If it matters, I'm trying to loop through site.static_files and only capture the files whose basename matches a variable passed in through an include.

You can use the variable directly as an expression:
{% if file.basename contains basename %}
The {{ syntax is only used to print expressions from markup.

Related

Set twig variable to json file as an include

I have created twig templates and save all the content in a jsonfile. Like this:
Json Data:
{% set contentElements = {
"json structur": {....}
"json structur": {....}
%}
Unfortunately over the years the json files has become bigger and bigger.
So i want to splitt the json data into snippets.
It is possible to set the variable contentElements to an include?
It is not working but something like this:
{% set contentElements = include"content.json "%}
Its an static HTML Project.
To capture chunks of text it is better to use the {% set var %}/{% endset %} tag. This allows you to assign "larger" amount of data to a variable. It's also possible to pass content from another file to the variable this way in combination with include.
{% set json %}
{% include "content.json" %}
{% endset %}
{{ json }}
(sidenote: Content captured as chunk is being treated as safe)

Jinja join list of strings if substring match

Is it possible to do something like this in jinja2:
my_list = ['foo1', 'bar1', 'foo2', 'bar2'] # could be any number of foo's and bar's
[i for i in my_list if 'foo' in i]
I was looking at map and join, something like:
{% my_list|map('???')|join(' ') %}
But I can't find a filter that would like me do any sort of wildcard searches. The two closest ones look like 'sameas' and 'equalto' but those don't quiet work.
If you're using a recent version of Jinja2 (2.7 or later), there's a new filter called 'select', which seems to do what you want. jinja.pocoo.org/docs/dev/templates/#select You will probably have to write your own test for this, and pass it into the jinja2 object when you instantiate it.
{{ foobars|select("test") }}
If you're on an even more up to date version (2.8 or later), you can also use block assignments http://jinja.pocoo.org/docs/dev/templates/#block-assignments
{% set my_foo_bars %}
{%- for item in my_list %}
{%- if item %}
{{item}}
{% endif -%}
{% endfor -%}
{% endset %}
If you're stuck on an older version (like the jinja2 from google app engine), you would probably be best doing the processing before passing it into the template, if possible.

Page variable in for loop file name

For a bilingual website, i have yaml data files for 2 languages.
files example:
en_services.yml
fr_services.yml
Variable example in my page:
---
lang: en
---
I want to loop trough the file with the lang as the prefix, something like that:
{% for service in site.data.{{ page.lang }}_services %}
{% endfor %}
This doesn't work, is there a way I can do that?
By the way, I don't think I can add subfolders in the _data folder, right?
Thanks.
While that doesn't work, if you are able to put them both in the same file (grouped under the appropriate language code) there is a solution.
This gist was for another example based on post authors, but your should be able to use the same setup using language codes instead of author names.
You should use:
site.data[{{ page.lang }} + '_services']
data[i] has key/value entries for all the files in your _data directory, and passing the string in, like so (for /_data/book.yaml):
site.data['book']
...totally works and opens up the possibility of concatenating strings inside the brackets with whatever variable you want. :)
{% capture thefile %}{{ page.lang }}_services{% end capture %}
{% for service in site.data[thefile] %}
...
{% endfor %}
Should do the trick.

How to I include all files from inside a directory in jinja2?

A normal jinja2 include looks like {% include 'directory/filename.html' %} but I am looking to do something like `{% include 'dropins/*.html' % where obviously the order would be alphabetically.
Is this possible? How?
You could pass a list of file names, then iterate over them:
{% for file_name in file_list %}
{% include file_name %}
{% endfor %}
And of course, inside file_list you should have already built your list of file names
file_list = ['dropins/file1.html', 'dropins/file2.html']
In Python, I would write some function that could discover all files inside that directory and then save their filenames to the list... or if you know the list just hard code them like above

Pass by reference or return value in jinja2

I have some code that I've been repeating a lot in one of my jinja2 templates. When I've been turning a string into a link I want to check to see if it has a trailing / at the end and if it does then truncate it. This is what I'd like it to look like.
{% macro remove_trailing_slash(path) %}
{% if path[-1:] == '/' %}
{{ path[:-1] }}
{% else %}
{{ path }}
{% endif %}
{% endmacro %}
The problem I'm having is figuring out how to pass the modified path back to the original caller. I can't seem to find a return statement in the jinja2 docs.
This is something that would be better done with a filter in Jinja, rather than a macro.
In my understanding macros are for reusing pieces of logic, they don't really map to functions in Python (it would be within the Jinja compiler's right to copy the contents of the macro to every place the macro is invoked). Filters, on the other hand, are designed to manipulate the template's data before it is passed on to the "output stream".
If you register a filter with the Jinja environment, then you can do something like this:
{{ one_url | remove_trailing_slash }}
{{ another_url | remove_trailing_slash }}
If you are doing this all over your templates, you may be better off sanitizing these values before even passing them off to your templates.
You can also create a macro to wrap this pattern:
{% macro link(url) %}
{{ url | remove_trailing_slash }}
{% endmacro %}