Skip to content

Possibility to evaluate without converting to ScriptArray #350

@SvenMitlehner

Description

@SvenMitlehner

If the result of a template evaluation is of any type that implements IEnumerable the result is automatically converted to a ScriptArray. I see the reasoning behind it and normally that is completely fine.

But there are cases where i want to explicitly keep the type instead of converting it to ScriptArray.

In the simple example below i would like the result to be of type Dictionary, but instead it is converted to ScriptArray of KeyValuePairs.

var so = new Scriban.Runtime.ScriptObject();
so.SetValue("test", new Dictionary<string, string>() { { "key1", "value1" } }, false);
var evaluated = Template.Parse("{{test}}").Evaluate(new TemplateContext(so));

At the moment I'm "solving" this problem by using

var evaluated = Template.Parse("{{test | outsmart_scriban}}").Evaluate(new TemplateContext(so));
if (evaluated is OutsmartScribanWrapper w)
{
    evaluated = w.o;
}
...

public static OutsmartScribanWrapper OutsmartScriban(object o)
{
    return new OutsmartScribanWrapper(o);
}

...

public class OutsmartScribanWrapper
{
  public object o;

  public OutsmartScribanWrapper(object o)
  {
      this.o = o;
  }
}

That works, but obviously is not a very elegant solution.

Would it be possible to have an optional parameter to the Evaluate method for disabling the automatic conversion toScriptArray or a second Evaluate method which does not do the automatic conversion?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions