Hey Dust community! ๐ Found something interesting about nested blocks! When trying to use variables in CURL URLs within WHILE or MAP/REDUCE structures, we get a templating error. For example, in this sequence:
CODE_nested (sets some variables)
CURL_nested (tries to use those variables in URL)
We get: "Templating error: Variable not found in context while rendering 'url'" ๐ Anyone else encountered this? Any workarounds you've found? Thanks! ๐
Hi Zoรฉ. I think what's happening is that your CODE_nested block actually returns an array of all the values so far. So in the URL, you could write something like {{CODE_nested[0].yourvar}} . But this does seem quite quirky, so I'm also curious how this would be used in practice. I assume that your intent is to use whatever the code block returned in the same iteration of the while?
Hello David! Thank you for the helpful insight about array indexing. You're absolutely right about the use case. Here's what I'm trying to achieve: I need to call 20+ API endpoints with different IDs. I was exploring two approaches:
WHILE loop to process each ID sequentially
MAP/REDUCE to handle a list of IDs in parallel
The main challenge I encountered was passing the current item's ID into the CURL URL within these loop structures.
Here's the working MAP/REDUCE structure I used: 1. CODE_PREPARE_ARRAY - Flattens nested data into array 2. MAP_PROCESS from that array 3. CODE_CURRENT_ITEM - Uses env.map.iteration to extract current item data 4. CURL_API_CALL - Uses {{CODE_CURRENT_ITEM.variable}} syntax directly 5. REDUCE_PROCESS collects results The intermediate CODE block with env.map.iteration works! You can access variables directly without [0] indexing.
Nice job in finding something that works! I guess MAP/REDUCE behaves differently from WHILE. For what it's worth, here is a tricky way to get the current item in the WHILE case, by setting the URL as follows: {% set last_element = CODE_NESTED | last %}somewhere.com?q={{last_element.variable}} So basically telling it to use the last element of the array, which is the current one. But yeah, that's quite dirty!