VolkovLabs / business-text

Dynamic, data-driven text for @grafana
https://docs.volkovlabs.io
Apache License 2.0
79 stars 14 forks source link

each iterator name {{this}} #343

Closed melquelima closed 1 month ago

melquelima commented 1 month ago

didnt found a way to change iterator name or give it a secondary name once i use 2 for each statements one inside another

{{#each data as this1}} {{this1.col1}} {{#each (someHelper data) as this2}} {{this1.col1}} {{this2.col1}} {{/each}}

{{/each}}

vitPinchuk commented 1 month ago

@melquelima Thanks for the question.

If I understand the question correctly, please apply the solution described below. In Handlebars.js, you can use different iterator names within nested {{#each}} statements by using custom block parameter names. block-parameters

Content code:

{{#each data as |outerItem|}}
<div style="padding:25px">
  <div style="color:green">{{outerItem.title}}</div>
  <div style="color:red">{{date outerItem.pubDate 'MMM, DD YYYY'}}</div>
  <div style="color:blue">{{outerItem.description}}</div>
</div>
  {{#each (someHelper outerItem) as |innerItem|}}
    <!-- <p style="font-size:16px">{{outerItem.link}}</p> -->
    <p style="font-size:12px">{{innerItem}}</p>
  {{/each}}
{{/each}}

Based on your question, we need register someHelper

JS code Before Content Rendering:

/*
  * console.log('context >>',context)
  * To check context
  */
console.log('context >>', context)

context.handlebars.registerHelper("someHelper", (data) => {

   /*
    * console.log('data >>',data)
    * To check data
    */

   /* 
    * Modified your data if necessary
    *
    */
  return data.category
});

The example shows test data, please adapt the solution for your data. image