Set twig variable to json file as an include - json

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)

Related

Using a variable with Jekyll's contain operator

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.

Liquid dynamic filename is not accepted

When i use
{% include folder1/folder1_1/img.jpg %}
it works perfectly, But when i try to generate the filename dynamically let's say :
{%capture filename %} {{'folder1/folder1_1/'}}{{ images[0] }}{{ '.jpg' }}{% endcapture %}
{% include {{ filename }} %}
with images[0] = 'img' for example, i get the error that says :
Liquid Exception: Invalid syntax for include tag. File contains
invalid characters or sequences ...
I Don't understand why including file by providing the complete path(static path) works whereas generating the filename dynamically won't work !
Any help would be more than appreciated.
After more research on the internet it seems that dynamic filename paths can't be added due to the fact that the included files are calculated and added at the compilation phase and not at run time phase.
And compilation phase means dynamic paths aren't yet recognized.
Maybe more luck with :
{% capture filename %}folder1/folder1_1/'{{ images[0] }}.jpg'{% endcapture %}
{% include {{ filename }} %}

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