Access a custom C# Dictionary Iterator in Scriban

90 views Asked by At

I got a Dictionary<string, object> where I want to format the object on the fly. This works in the console, the problem is that I need to access those values in my Scriban template.

That's how I implemented my custom Iterator:

public class Workflow {
    public Dictionary<string, object>? Variables { get; set; }
    public IEnumerable<(string key, string value)> GetVariables() {
        foreach (var kvp in Variables) {
            yield return (kvp.Key, FormatUtils.FormatVariable(kvp.Value));
        }
    }
}

This is how I pass it to my renderer:

template.Render(new {
     variables = workflow.GetVariables()
});

And then I want to access my keys and values in my scriban template:

{{~ for var in variables ~}}
  {{ var.key }}: {{ var.value }} 
{{~ end ~}}

This worked when I accessed Variables directly. Now I just got no outputs, as key and value is not defined (this is the output for variables in Scriban):

[(JOB_NUMBER, 42), (JOB_DESC, "New Car")]

It looked like this with Variables passed instead:

[{key: "JOB_NUMBER", value: 42}, {key: "JOB_DESC", value: "New Car"}]

How do I fix the access to my values?

0

There are 0 answers