How can I assign a value to a data variable #519

Closed
opened 2025-12-29 15:25:49 +01:00 by adam · 5 comments
Owner

Originally created by @Dunga999 on GitHub (May 29, 2023).

How can I assign a value to a variable that I can use latter when templating.
Looking for examples I came with something like:

{{#assign 'data'}}{{array 'a' 'b' 'c'}}{{/assign}}
...
{{#each ({{data}}) as |value|}}
   {{value}}
{{/each}}

But I can't make it work. Somehow #assign is not recognized.
Is this the correct way to do it?

By the way, You have made a great tool.
Thank you

Originally created by @Dunga999 on GitHub (May 29, 2023). How can I assign a value to a variable that I can use latter when templating. Looking for examples I came with something like: ``` {{#assign 'data'}}{{array 'a' 'b' 'c'}}{{/assign}} ... {{#each ({{data}}) as |value|}} {{value}} {{/each}} ``` But I can't make it work. Somehow #assign is not recognized. Is this the correct way to do it? By the way, You have made a great tool. Thank you
adam added the question label 2025-12-29 15:25:49 +01:00
adam closed this issue 2025-12-29 15:25:49 +01:00
Author
Owner

@StefH commented on GitHub (May 30, 2023):

I don't know if "#assign" works.

But you can also just define a Data field with some static data.

See this issue for more details
https://github.com/WireMock-Net/WireMock.Net/issues/892

@StefH commented on GitHub (May 30, 2023): I don't know if "#assign" works. But you can also just define a Data field with some static data. See this issue for more details https://github.com/WireMock-Net/WireMock.Net/issues/892
Author
Owner

@Dunga999 commented on GitHub (May 30, 2023):

Yes, I have crossed that option, but I need a more dynamic (not static) data.
My full need is that I'm receiving a request with an ID and with that ID I need to query a DB to gather specific data for the response.
My issue is how to pass this dynamic data when templating.

@Dunga999 commented on GitHub (May 30, 2023): Yes, I have crossed that option, but I need a more dynamic (not static) data. My full need is that I'm receiving a request with an ID and with that ID I need to query a DB to gather specific data for the response. My issue is how to pass this dynamic data when templating.
Author
Owner

@StefH commented on GitHub (May 30, 2023):

If you have an ID in the request parameter or body, you can use get that value and return it in the response.

See also
https://github.com/WireMock-Net/WireMock.Net/wiki/Response-Templating

@StefH commented on GitHub (May 30, 2023): If you have an ID in the request parameter or body, you can use get that value and return it in the response. See also https://github.com/WireMock-Net/WireMock.Net/wiki/Response-Templating
Author
Owner

@StefH commented on GitHub (Jun 13, 2023):

@Dunga999
Does my proposed solution work for you?

@StefH commented on GitHub (Jun 13, 2023): @Dunga999 Does my proposed solution work for you?
Author
Owner

@Dunga999 commented on GitHub (Jun 13, 2023):

Hi @StefH,

Thanks for the follow up.
As I said, I need to handle dynamic data, so your solution didn't directly attend my needs, but pointed me a way to handle the requirement.
I created an helper to save the dynamic data in the Data field so I could access it in the templating.
I think I didn't gave you the full view of the dynamic data requirement, but I implemented something like this:

{{save 'invoice' (query 'connectionId' 'SELECT col1, col2 FROM Invoice WHERE InvoiceId=@id' id=(request.pathsegments.[3])) }}
...
{{#each (Data.invoice) as |item|}}
...
"invoiceId": "{{item.col1}}",
...
{{/each}}

Being, "save" and "query" the helpers I created.

Again, thank you for the suggestion and for this amazing tool

@Dunga999 commented on GitHub (Jun 13, 2023): Hi @StefH, Thanks for the follow up. As I said, I need to handle dynamic data, so your solution didn't directly attend my needs, but pointed me a way to handle the requirement. I created an helper to save the dynamic data in the Data field so I could access it in the templating. I think I didn't gave you the full view of the dynamic data requirement, but I implemented something like this: > {{save 'invoice' (query 'connectionId' 'SELECT col1, col2 FROM Invoice WHERE InvoiceId=@id' id=(request.pathsegments.[3])) }} > ... > {{#each (Data.invoice) as |item|}} > ... > "invoiceId": "{{item.col1}}", > ... > {{/each}} Being, "save" and "query" the helpers I created. Again, thank you for the suggestion and for this amazing tool
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#519