Currently using OctoberCMS and Twig to get dynamic data from a json file. The basic idea is this:
- Have a sidebar with 3 links in it
- The Sidebar links are generated randomly on load
- There are certain links that need to show up more often than others and some that are lower in priority
Here's some example code to start with so you have an idea of what I'm trying to achieve:
"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 %}
I obviously need to randomize the results of the array and somehow have the Tier1 items show up most often, Tier2 show up often, and Tier3 to show up least often. The|translations
filter just helps point to the json file with the appropriate translations - just in case it confuses you.
So far, I've tried to convert the answers from here and here, but with no luck.
I have a feeling I may not be translating code for Twig in the most ideal way possible, but I'm struggling to figure it out. Any assistance would be appreciated!