如何使用 Twig 按权重生成随机结果?
How do you generate random results by weight with Twig?
目前正在使用 OctoberCMS 和 Twig 从 json 文件中获取动态数据。基本思路是这样的:
- 有一个包含 3 个链接的边栏
- 边栏链接是随机生成的
正在加载
- 有些链接需要比以往更频繁地出现
其他和一些优先级较低的
这里有一些示例代码可以让您了解我要实现的目标:
"wiki": {
"resources": {
"items": [
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 1
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 3
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 2
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 2
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 2
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 1
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 3
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 1
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 2
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 3
}
]
}
}
{% for item in 'wiki.resources.items'|translations %}
<div class="ressources__item col-12 col-md-4 col-lg-12">
<a href="{{ item.href }}">
<h3 class="ressources__sub">{{ item.h3 }} </h3>
</a>
<p>{{ item.p }}</p>
</div>
{% endfor %}
我显然需要随机化数组的结果,并以某种方式让第 1 层项目最常出现,第 2 层经常出现,第 3 层最不经常出现。 |translations
过滤器只是帮助指向具有适当翻译的 json 文件 - 以防它让您感到困惑。
到目前为止,我已经尝试转换 here and here 的答案,但没有成功。
我觉得我可能没有以最理想的方式翻译 Twig 的代码,但我正在努力弄清楚。如有任何帮助,我们将不胜感激!
用这个解决了:
{% set linkWeight = 0 %}
{% for item in 'wiki.resources.items'|translations %}
{% set linkWeight = linkWeight + item.weight %}
{% endfor %}
{% set randomWeight = random(1,linkWeight) %}
{% set linkArr = [] %}
{% for item in 'wiki.resources.items'|translations %}
{% set randomWeight = randomWeight - item.weight %}
{% if randomWeight <= 0 %}
{% set linkArr = linkArr|merge([item]) %}
{% endif %}
{% endfor %}
{% for item in linkArr[:3] %}
<div class="ressources__item col-12 col-md-4 col-lg-12">
<a href="{{ item.href }}">
<h3 class="ressources__sub">{{ item.h3 }} </h3>
</a>
<p>{{ item.p }}</p>
</div>
{% endfor %}
基本上,我将 JSON 文件中的 "tier" 键更改为 "weight" 并为之前的 Tier1 添加了一个更大的数字,为 Tier3 添加了相反的数字。
如果您对它的工作原理感到好奇,here's the article 它帮助我从逻辑上理解了我需要实现的目标。这里唯一的区别是我用加权随机选择制作了另一个数组,这样我一次只能显示选择中的 3 个(如下所示:{% for item in linkArr[:3] %}
)
目前正在使用 OctoberCMS 和 Twig 从 json 文件中获取动态数据。基本思路是这样的:
- 有一个包含 3 个链接的边栏
- 边栏链接是随机生成的 正在加载
- 有些链接需要比以往更频繁地出现 其他和一些优先级较低的
这里有一些示例代码可以让您了解我要实现的目标:
"wiki": {
"resources": {
"items": [
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 1
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 3
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 2
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 2
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 2
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 1
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 3
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 1
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 2
},
{
"href": "[link]",
"h3": "[Title]",
"p": "[Description]",
"tier": 3
}
]
}
}
{% for item in 'wiki.resources.items'|translations %}
<div class="ressources__item col-12 col-md-4 col-lg-12">
<a href="{{ item.href }}">
<h3 class="ressources__sub">{{ item.h3 }} </h3>
</a>
<p>{{ item.p }}</p>
</div>
{% endfor %}
我显然需要随机化数组的结果,并以某种方式让第 1 层项目最常出现,第 2 层经常出现,第 3 层最不经常出现。 |translations
过滤器只是帮助指向具有适当翻译的 json 文件 - 以防它让您感到困惑。
到目前为止,我已经尝试转换 here and here 的答案,但没有成功。
我觉得我可能没有以最理想的方式翻译 Twig 的代码,但我正在努力弄清楚。如有任何帮助,我们将不胜感激!
用这个解决了:
{% set linkWeight = 0 %}
{% for item in 'wiki.resources.items'|translations %}
{% set linkWeight = linkWeight + item.weight %}
{% endfor %}
{% set randomWeight = random(1,linkWeight) %}
{% set linkArr = [] %}
{% for item in 'wiki.resources.items'|translations %}
{% set randomWeight = randomWeight - item.weight %}
{% if randomWeight <= 0 %}
{% set linkArr = linkArr|merge([item]) %}
{% endif %}
{% endfor %}
{% for item in linkArr[:3] %}
<div class="ressources__item col-12 col-md-4 col-lg-12">
<a href="{{ item.href }}">
<h3 class="ressources__sub">{{ item.h3 }} </h3>
</a>
<p>{{ item.p }}</p>
</div>
{% endfor %}
基本上,我将 JSON 文件中的 "tier" 键更改为 "weight" 并为之前的 Tier1 添加了一个更大的数字,为 Tier3 添加了相反的数字。
如果您对它的工作原理感到好奇,here's the article 它帮助我从逻辑上理解了我需要实现的目标。这里唯一的区别是我用加权随机选择制作了另一个数组,这样我一次只能显示选择中的 3 个(如下所示:{% for item in linkArr[:3] %}
)