Help with getting a value from a template

Hi, i’m working in a template but i cant see where is the problem. idcounter should contain 142 as result. I’m trying to get the platform_id data if equal to 13, 69, 142 or 289.

Any help will be appreciated

{% set idplataforma = {"alternate_titles":[],"description":null,"game_id":99919,"genres":[{"genre_category":"Basic Genres","genre_category_id":1,"genre_id":1,"genre_name":"Action"},{"genre_category":"Perspective","genre_category_id":2,"genre_id":7,"genre_name":"1st-person"},{"genre_category":"Gameplay","genre_category_id":4,"genre_id":149,"genre_name":"Roguelike"},{"genre_category":"Gameplay","genre_category_id":4,"genre_id":22,"genre_name":"Shooter"},{"genre_category":"Interface/Control","genre_category_id":7,"genre_id":168,"genre_name":"Direct control"},{"genre_category":"Setting","genre_category_id":10,"genre_id":78,"genre_name":"Fantasy"}],"moby_score":7.4,"moby_url":"https://www.mobygames.com/game/99919/superhot-mind-control-delete/","num_votes":4,"official_url":"http://superhotgame.com","platforms":[{"first_release_date":"2020-07-16","platform_id":1,"platform_name":"Linux"},{"first_release_date":"2017-12-07","platform_id":3,"platform_name":"Windows"},{"first_release_date":"2020-07-16","platform_id":74,"platform_name":"Macintosh"},{"first_release_date":"2021-02-25","platform_id":140,"platform_name":"Windows Apps"},{"first_release_date":"2020-07-16","platform_id":141,"platform_name":"PlayStation 4"},{"first_release_date":"2020-07-16","platform_id":142,"platform_name":"Xbox One"},{"first_release_date":"2020-08-18","platform_id":281,"platform_name":"Stadia"}],"sample_cover":{"height":215,"image":"https://cdn.mobygames.com/covers/9822818-superhot-mind-control-delete-windows-front-cover.jpg","platforms":["Windows"],"thumbnail_image":"https://cdn.mobygames.com/76c1be40-ac0a-11ed-962d-02420a000135.webp","width":460},"sample_screenshots":[],"title":"Superhot: Mind Control Delete"}%}

    {%  set id = namespace() %}
    {% set id.counter = 1 %}
    {% for i in idplataforma['platforms'] %}
      {% if i.platform_id == 13  %}
       {% set id.counter = 13 -%}
      {% elif i.platform_id == 69 %}
       {% set id.counter = 69 -%}
      {% elif i.platform_id == 142 %}
       {% set id.counter = 142 -%}
      {% elif i.platform_id == 289 %}
       {% set id.counter = 289 %}
      {% else %} 
       {% set id.counter = 0 %}
      {% endif %}
    {% endfor %}
    
    {{ id.counter }}

sorry if i’m missing something obvious, but where is platform_id specified in idplataforma? i see genre_id, but not platform_id.

if it is supposed to be genre_id, the reason it’s 0 at the end is because the last genre_id is 281. so it his your final ‘else’ clause setting id.counter to 0.

in that part, inside platforms, the second value is platform_id

if a write {{i.platform_id}} inside the loop i get the platform_id list with 142 but i do not know why the if do not work

[{'first_release_date': '2020-07-16', 'platform_id': 1, 'platform_name': 'Linux'}, {'first_release_date': '2017-12-07', 'platform_id': 3, 'platform_name': 'Windows'}, {'first_release_date': '2020-07-16', 'platform_id': 74, 'platform_name': 'Macintosh'}, {'first_release_date': '2021-02-25', 'platform_id': 140, 'platform_name': 'Windows Apps'}, {'first_release_date': '2020-07-16', 'platform_id': 141, 'platform_name': 'PlayStation 4'}, {'first_release_date': '2020-07-16', 'platform_id': 142, 'platform_name': 'Xbox One'}, {'first_release_date': '2020-08-18', 'platform_id': 281, 'platform_name': 'Stadia'}]

ah, got it. still, what i said at the end holds… the last platform_id is 289 and as such the last gets set to 0

put the below into your dev-tools->templates page and it’ll highlight what’s happening:

{% set idplataforma = {"alternate_titles":[],"description":null,"game_id":99919,
"genres":[{"genre_category":"Basic Genres","genre_category_id":1,"genre_id":1,"genre_name":"Action"},
          {"genre_category":"Perspective","genre_category_id":2,"genre_id":7,"genre_name":"1st-person"},
          {"genre_category":"Gameplay","genre_category_id":4,"genre_id":149,"genre_name":"Roguelike"},
          {"genre_category":"Gameplay","genre_category_id":4,"genre_id":22,"genre_name":"Shooter"},
          {"genre_category":"Interface/Control","genre_category_id":7,"genre_id":168,"genre_name":"Direct control"},
          {"genre_category":"Setting","genre_category_id":10,"genre_id":78,"genre_name":"Fantasy"}],
          "moby_score":7.4,"moby_url":"https://www.mobygames.com/game/99919/superhot-mind-control-delete/",
          "num_votes":4,"official_url":"http://superhotgame.com",
          "platforms":[{"first_release_date":"2020-07-16","platform_id":1,"platform_name":"Linux"},
          {"first_release_date":"2017-12-07","platform_id":3,"platform_name":"Windows"},
          {"first_release_date":"2020-07-16","platform_id":74,"platform_name":"Macintosh"},
          {"first_release_date":"2021-02-25","platform_id":140,"platform_name":"Windows Apps"},
          {"first_release_date":"2020-07-16","platform_id":141,"platform_name":"PlayStation 4"},
          {"first_release_date":"2020-07-16","platform_id":142,"platform_name":"Xbox One"},
          {"first_release_date":"2020-08-18","platform_id":281,"platform_name":"Stadia"}],
          "sample_cover":{"height":215,"image":"https://cdn.mobygames.com/covers/9822818-superhot-mind-control-delete-windows-front-cover.jpg","platforms":["Windows"],
          "thumbnail_image":"https://cdn.mobygames.com/76c1be40-ac0a-11ed-962d-02420a000135.webp","width":460},
          "sample_screenshots":[],"title":"Superhot: Mind Control Delete"}%}

    {%  set id = namespace() %}
    {% set id.counter = 1 %}
    {% for i in idplataforma['platforms'] %}
       {{i}}
       {{ i.platform_id}}
      {% if i.platform_id == 13  %}
       {% set id.counter = 13 -%}
      {% elif i.platform_id == 69 %}
       {% set id.counter = 69 -%}
      {% elif i.platform_id == 142 %}
       {% set id.counter = 142 -%}
      {% elif i.platform_id == 289 %}
       {% set id.counter = 289 %}
      {% else %} 
       {% set id.counter = 0 %}
      {% endif %}
      id.counter is being set to {{ id.counter }}
    {% endfor %}
    
final id.counter    {{ id.counter }}


if i remove the else works fine and id.counter shows 142 as should be. But how can i set a value 0 if none of the conditions are 13, 69, 142 or 289?

maybe the question is what you want it to do. right now id.counter gets set to the very last thing platform_id that matches (or 0 if the last thing doesn’t match).

is your intent to find the very last platform_id value?

if it’s to return the first hit, then something like this? debugging string still included… use this for devtools. remove the debugging strings when ready…

  {%  set id = namespace() %}
    {% set id.counter = 0 %}
    {% for i in idplataforma['platforms'] %}

      {% if id.counter == 0 %}
         {{i}}
         {{ i.platform_id}}
        {% if i.platform_id == 13  %}
         {% set id.counter = 13 -%}
        {% elif i.platform_id == 69 %}
         {% set id.counter = 69 -%}
        {% elif i.platform_id == 142 %}
         {% set id.counter = 142 -%}
        {% elif i.platform_id == 289 %}
         {% set id.counter = 289 %}
        {% else %} 
         {% set id.counter = 0 %}
        {% endif %}
        id.counter is being set to {{ id.counter }}
      {% endif %}     
    {% endfor %}
1 Like

i.m trying to find if platform_id match one of this values: 13, 69, 142 or 289 if match save the last value that matches in id.counter. if none of these numbers are in the list of platform.id i want to set id.counter to 0

after i posted initially, i guessed at what you were intending and added code for you to try. let me know how that goes.

Working perfect. Thank you very much for your help :slight_smile:

great… then in that case, try this:

    {% set id = namespace() %}
    {% set id.counter = 0 %}
    {% for i in idplataforma['platforms'] %}
      {% if id.counter == 0 and i.platform_id in [13, 69, 142, 289]  %}
         {% set id.counter = i.platform_id -%}
      {% endif %}     
    {% endfor %}
    
    {{ id.counter }}

More than perfect. Very grateful for dedicating your time and work.

You can do this in a single line without looping also. Either of these will result in the same answer but they go about it in different ways. If you need to access other information in the matched dictionary, the second one can be easily modified to extract other values.

{{ idplataforma['platforms'] | map(attribute='platform_id') | select('in',  [13, 69, 142, 289]) | list | last }}
{{ (idplataforma['platforms'] | selectattr('platform_id', 'in',  [13, 69, 142, 289]) | list | last)['platform_id'] }}

Nice, thank you :blush: