How to pass a concatenated string to HTML using Jekyll include? - html

I have to make a string of values separated by semicolons and pass this string to a script in HTML include to then parse and output the result.
My jekyll markdown page:
---
layout: page
title: Our team
permalink: /team
---
{% assign cfhandles=""%}
{% for person in site.data.team-handles %}
{{ cfhandles | append: person.handle }}
{{ cfhandles | append: ";" }}
{% endfor %}
{% include load-ratings.html cfhandles=cfhandles %}
My load-ratings.html:
<script>
let url = "https://codeforces.com/api/user.info?handles=";
let handles = "{{ include.cfhandles }}";
console.log(handles);
url = url + handles;
console.log(url);
async function load() {
let obj = await (await fetch(url)).json();
console.log(obj);
for (let cur_user of obj["result"]) {
let cur_handle = cur_user["handle"];
let user_rating = cur_user["rating"];
document.getElementById(cur_handle.toLowerCase()).innerHTML = "Рейтинг: " + user_rating;
}
}
load();
</script>
My team-handles.yml:
- handle: bob1
name: Bob
- handle: alice2
name: Alice
When I open the developer console, it shows me that in JS the handles variable is empty (but it shouldn't be like that). How do I pass the needed string to the file so it shows correctly?
I already tried using {%- -%} Liquid tags, but it didn't change the result.

Try this:
{% assign cfhandles=""%}
{% for person in site.data.team-handles %}
{% assign cfhandles = cfhandles | append: person.handle | append: ";" %}
{% endfor %}
{% include load-ratings.html cfhandles=cfhandles %}
The code basically assigns the new strings bob1; and alice2; to the existing variable.

Related

concat string and for iteration using jinja2

I would like to create a variable name using a concatenation of a string and for iteration:
{% for iteration in my_array %}
{% set my_var = 'my_string_' + loop.index0|string %}
{{ my_var }}
{% endfor %}
in my python file with data:
templateVars={
'my_string_0': 'test with 0',
'my_string_1': 'test with 1'
}
outputText = template.render(templateVars)
but I don't get 'test with 0' I get 'my_string_0'
'my_string_' is just a string, not a variable. Since your "variable" names are just named with incremental numbers, you should make it a list instead:
outputText = template.render(my_array=['test with 0', 'test with 1'])
so you can iterate through the list in the template:
{% for item in my_array %}
{{ item }}
{% endfor %}

How to access field names of javascript object in HTML passed in from backend?

Example:
var obj={name:bob,
}
I want to access not the value of name i.e. bob instead an array containing keys of obj like [name].
<h1>{{ pagename|title }}</h1>
<ul>
{% for author in collections %}
<li >
{{ author.uno }}
{{ author.subject }}
<script>
var array = Object.keys(author).map((key) => key) ;
document.write('<p>' + array + '</p>');
</script>
{% for element in author %}
<li >
{{element }}
</li>
{% endfor %}
</li>
{% endfor %}
</ul>
Here collections is an array of objects passed in from backend i.e. nodejs.
Author is a javascript object.
I have tried getting desired result using logic inside script tag.
But it is not printing anything on webpage.
I have also tried placing {{}} at different positions without getting fruitful results.
update: I forgot you're using swig-template. Here is my suggestion:
//backend :
const author = { /* author object */ };
author.keys = Object.keys(author).join(', ');
swig.renderFile('/path/to/template.html',{ author });
and then, put it in template.htm;
{{ author.subject }}
{{ author.keys }}
{% for element in author %}
<li >
{{element }}
</li>
{% endfor %}

Jinja2: use template to build a string variable

Jinja2 supports very useful filters to modify a string, eg.
{{ my_string|capitalize }}
What about you want to build the input string? When the string is simple you can always use
{% set my_string = string_1 + string_2 %}
{{ my_string|capitalize }}
But it would be wonderful to actually build this string using templates, just like
{% set my_string = "{{ 'a' }}b{{ 'c' }}" %}
{{ my_string|capitalize }}
that would output Abc..
Did I miss something?
Answer exists in Jinja 2.8, as documented here
The answer is
{% set my_string %}
{{ 'a'}}b{{ 'c' }}
{% endset %}

jekyll assign concat in a loop?

I would like to organize a page based on the number of pages that pass a filter.
I have tried to append truthy pages to a collection but it doesn't work.
{% assign homepage_posts = [] %}
{% for my_page in site.pages %}
{% if my_page.homepage %}
{% assign homepage_posts = homepage_posts | concat: [my_page] %}
{% endif %}
{% endfor %}
<h1>size{{homepage_posts.size}}</h1>
<h1>{{homepage_posts}}</h1>
This is not working. Does concat only work with strings?
Jekyll will use Liquid 4 soon. But, for now, no concat.
In your case you can :
Create an empty array (bracket notation doesn't work in liquid) : {% assign homepage_posts = "" | split:"/" %}
{{ homepage_posts | inspect }} --> output : []
And push elements in it :
{% for my_page in site.pages %}
{% if my_page.homepage %}
{% assign homepage_posts = homepage_posts | push: mypage %}
{% endif %}
{% endfor %}
{{ homepage_posts | inspect }}
concat filter only works with arrays and will be available in Jekyll when it upgrades to Liquid 4.*:
concat
Concatenates (combines) an array with another array. The resulting
array contains all the elements of the original arrays. concat will
not remove duplicate entries from the concatenated array unless you
also use the uniq filter.
To filter pages containing a specific attribute (in this case homepage: true) you can use a where filter.
Having a page with front matter:
---
homepage: true
---
Then you can have the pages with the homepage: true attribute like:
{% assign homepages = site.pages | where:"homepage","true" %}

How to concatenate / append a string to another one in Jekyll / Liquid?

To be clear, assuming:
{% assign my_var = "123" %}
{% assign another_var = "456" %}
I would like to append string to my_var to get something like 123 - 456
What I have tried so far:
{% assign my_var = my_var + " - " + another_var %}
You could use the capture logic tag:
{% capture new_var %}{{ my_var }} - {{ another_var }}{% endcapture %}
It is also possible to use the append filter, as Ciro pointed:
{% assign new_var = my_var | append: ' - ' | append: another_var %}
append: filter
This is more convenient than capture for short concatenations:
{% assign x = 'abc' %}
{% assign y = 'def' %}
{% assign z = x | append: ' - ' | append: y %}
{{ z }}
Output:
abc - def
Tested on jekyll 3.0.4 (github-pages 75).
All the answers so far are correct, but they fail to mention that you can also inline the append instead of having to assign a new variable:
Link