如何使用Jekyll include传递一个连接的字符串到HTML ?



我必须制作一个以分号分隔的值字符串,并将该字符串传递给HTML include中的脚本,然后解析并输出结果。

我的jekyll降价页面:

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

我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>

我team-handles.yml:

- handle: bob1
name: Bob
- handle: alice2
name: Alice

当我打开开发控制台时,它告诉我在JS中handles变量是空的(但它不应该是那样的)。我如何将所需的字符串传递给文件,使其正确显示?

我已经尝试使用{%- -%}Liquid标签,但它没有改变结果。

试试这个:

{% assign cfhandles=""%}
{% for person in site.data.team-handles %}
{% assign cfhandles = cfhandles | append: person.handle | append: ";" %}
{% endfor %}
{% include load-ratings.html cfhandles=cfhandles %}

代码基本上将新的字符串bob1;alice2;赋值给现有的变量。

相关内容

  • 没有找到相关文章

最新更新