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 }} %}
Related
I get the following error:
Invalid syntax for include tag. File contains invalid characters or sequences: (ArgumentError)
../ossvlcm/go.md
Valid syntax:
{% include_relative file.ext param='value' param2='value' %}
The line of code throwing the error:
{% assign temp_url = nPath | append: chapter.file %}{% capture my_include %}{% include_relative {{ temp_url | append: '.md' }} %}{% endcapture %}
and nPath is create with:
{% if page.ePath %}
{% assign nPath = '../ossclvm/' %}
{% endif %}
What I'm trying to do is include a file in from the a different folder from where the index.md resides. It seems to get hung up on the '../' characters but I've seen examples that use this.
So what exactly is wrong and how do I access other content from different folders?
Found out that .. is not supported in Jekyll/Liquid.
I reformatted the folder structure to make this work, index files in the parent folders to the content files.
Not a great solution
I'm adding some globals in my handler that should be available to all templates, including macros.
jinja_env.globals.update({
"SOME_CONSTANT": SOME_CONSTANT_1,
})
This works fine when I use it inside a macro like this:
{% macro is_some_constant(some_str) -%}
some_str == SOME_CONSTANT
{%- endmacro %}
But I get an undefined error when I try to use a global as a default arg to the macro:
{% macro test_something(something=SOME_CONSTANT) -%}
...
{%- endmacro %}
Result: NameError: global name 'l_SOME_CONSTANT' is not defined
Is this expected? I found this related issue which is claimed to be fixed by this PR, but still running into this. The workaround mentioned in the first link does work, but is kind of silly and annoying:
{% set SOME_CONSTANT = SOME_CONSTANT %}
{% macro test_something(something=SOME_CONSTANT) -%}
...
{%- endmacro %}
Oh, I realized I'm on jinja2 v2.8, and the fix PR was merged into 2.9 (I think). So perhaps that fixes the problem.
For now, I'll just use the hacky workaround.
I have a page say index.html and a file with liquid code called tags. I want to pass a parameter which I specify in index.html to a liquid call in tags. Namely,
index.html
<div>
{% include tags param="site.categories" %}
</div>
Tags
{% assign tags_list = {{ include.param }} %}
...
The {% assign tags_list = {{ include.param }} %} does not work for some reason. Is it possible to do so because this allows me to use tags file for multiple purposes? Instead of writing liquid calls in every page Iwould be able to just do {% include tags param="something" %}. Thanks.
Well, {% include tags param="site.categories" %} is not passing the site.categories hash but the "site.categories" string.
The right syntax is :
{% include tags.html param=site.categories %}
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 %}
I am building a Jekyll blog, and I have come across an issue with permalinks.
My permalinks to blog posts are set like this in
_config.yml:
permalink: /:page/:categories/:title
It outputs like this when navigating to a blog post:
http://localhost:4000/blog/travel/netherlands-trip-prequesites/
I have some static pages in the site: Blog, Travel
The variable page.url outputs this url: /blog/travel/netherlands-trip-prequesites
The code my navigation bar uses to highlight the current page (giving it an "active" class):
{% assign url = page.url|remove:'index.html' %}
{% for nav in site.navigation %}
{% if nav.href == url %}
<li class="active">{{nav.name}}</li>
{% else %}
<li>{{nav.name}}</li>
{% endif %}
{%endfor%}
It works great when navigating to static pages, however when I click a blog post it doesn't highlight the correct static page. (ex.: If i navigate to a blog post with the url /blog/smth/title it should automatically highlight "Blog" in my navigation. When I navigate to /travel/smth/title it should highlight "Travel")
What I'd like to do is to strip down the output of page.url to its first part. For example I'd like to stip the following output
/blog/travel/netherlands-trip-prequesites
down to
/blog/
Why? So I can use it to check which static page it belongs to and highlight it accordigly.
The easiest way is to use split:
{{ page.url | split:'/' | first }}
That will give you the URL content up to the first / character.
I managed to solve it with three filters:
{{ page.url | replace:'/',' ' | truncatewords: 1 | remove:'...' }}
page.url outputs: /page/cat/title, then replace removes the forward slashes producing: page cat title. truncatewords truncates the string down to one word, producing: page... (for some reason three dots gets inserted after the remaining word). After all this I only needed to remove those dots with remove and voilá, my final string: page.
Hope this helps someone.
The answer provided by PeterInvincible was almost perfect, however, there's no need to get piping to remove involved...
The following also will produce desired output
{{ page.url | replace:'/',' ' | truncatewords: 1,"" }}
And to save it to a variable use capture redirection
{{ capture url_base }}{{ page.url | replace:'/',' ' | truncatewords: 1,"" }}{{ endcapture }}
Which can be called via {{url_base}} or mixed with other processing calls.
Also for file paths instead of URLs page.dir works well if you're not using permalink settings for layout, check the gh-pages branch (specifically _includes/nav_gen.html for functional, though rough'round the edges, example) for hosted examples of similar code examples related to liquid syntax and other magic.
Edits & Updates
The above linked script is now live/mostly-working/modular and auto-serving parsed sub-directories viewed currently at the related https://s0ands0.github.io/Perinoid_Pipes/ project site providing examples of recursive parsing of directories. Including and modding for nearly any theme should be possible just check the commented section at the top for currently recognized commands that maybe passed at inclusion call... on that note of inclusion and modularization here's how to turn the above example code for directory parsing into a function
{% comment %}
# Save this to _include/dir_path_by_numbers.html
# import with the following assigning arguments if needed
# {% include dir_path_by_numbers.html directory_argument_path="blog" directory_argument_depth=1 %}
{% endcomment %}
{% assign default_arg_directory_path = page.url %}
{% assign default_arg_directory_depth = 1 %}
{% if directory_argument_path %}
{% assign directory_to_inspect = directory_argument_path %}
{% else %}
{% assign directory_to_inspect = default_arg_directory_path %}
{% endif %}
{% if directory_argument_depth %}
{% assign directory_to_inspect_depth = directory_argument_path %}
{% else %}
{% assign directory_to_inspect_depth = default_arg_directory_depth %}
{% endif %}
{% comment %}
# Defaults read and assigned now to output results
{% endcomment %}
{{ directory_to_inspect_depth | replace:'/',' ' | truncatewords: directory_to_inspect_depth,"" | remove_first: '/' | replace:' ','/' }}
The above should output directory path lengths of whatever size desired and maybe included as shown previously or if feeling adventurous try what's shown below; though for looping and recursive features look to the linked script for how I've worked around stack size restrictions.
{% capture dir_sub_path %}{{include dir_path_by_numbers.html directory_argument_path="blog" directory_argument_depth=1}}{% endcapture %}
Note above is just speculation, untested, and maybe more buggy than scripts tested and hosted publicly... in other words inspiration.
Simplest way would be using
if page.url contains
example:
<li class="{% if page.url contains '/docs/' %}current{% endif %}">
Docs