scriban / scriban

A fast, powerful, safe and lightweight scripting language and engine for .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Arrays at the root of a dynamic object are not evaluated.

DaveNatalieTripArc opened this issue · comments

Given this Json

{
        "name":"Beatles", 
        "people": [
            { "name": "John" },
            { "name": "Paul" },
            { "name": "George" },
            { "name": "Ringo" }  
        ]      
     }

and template that looks like this

<h1>{{name}}</h1>
<ul>
    {{ for person in people }}
   <li>Name: {{ person.name }}</li>
    {{ end }}
</ul>

If I try to render this using the following code,

var scribanTemplate = Template.Parse(template);
var expando = JsonConvert.DeserializeObject<ExpandoObject>(json);
var result = await scribanTemplate.RenderAsync(expando);

I'll get the following result. As you can see, the <ul> is empty, but the name was rendered correctly.

<h1>Beatles</h1>
<ul>
	
</ul>

If I add an additional layer of nesting at the root of my dynamic object, it seems work correctly.

var result = await scribanTemplate.RenderAsync(new { model =  expando });

{{ for person in model. People }}

Is this a bug, or am I doing something wrong?

I've tried deserializing using both Newtonsoft and STJ.

Scriban does not support ExpandoObject directly. You need to have one level of indirection at least.

You could also deserialize directly to a ScriptObject like I did here