简体   繁体   中英

How do you generate random results by weight with Twig?

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!

Solved it with this:

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

Basically, I changed the "tier" keys in the JSON file to "weight" and added a larger number for what was previously Tier1 and the opposite for Tier3.

If you're curious about how it works, here's the article that helped me make logical sense of what I needed to achieve. The only difference here is that I made another array with the weighted random selection so that I could only display 3 from the selection at a time (as can be seen here: {% for item in linkArr[:3] %} )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM