scriban icon indicating copy to clipboard operation
scriban copied to clipboard

String interpolation is not working when used in function call expression

Open Hominis06 opened this issue 1 year ago • 2 comments

I have declared .net function

public static class ResourceService
{        
    public static string Resource(string language, string key)
    {
        if (_generalResources.TryGetValue(language, out IReadOnlyDictionary<string, string> value) && value.ContainsKey(key))
        {
            return value[key];
        }
        else
        {
            return null;
        }
    }
}

And i have custom template, that have this section to render by scriban: "{{ $"{resource "ru-RU" $"General.{string.capitalize require_for_auto_test}"}" }}"

require_for_auto_test is a boolean variable, that comes as incoming arguments

When I try to render template with this code snippet

ScriptObject scribanObject = new ();
scribanObject.Import(arguments, null, null);
scribanObject.Import(typeof(ResourceService));

TemplateContext context = new ();
context.PushGlobal(scribanObject);

string renderResult = await scribanTemplate.RenderAsync(context);

And renderResult return not null or some other value from Resource function. Instead, renderResult returns "True". While debugging my c# code i discovered that scriban called imported function Resource without attempting to interpolate second argument (string key) of function (In C# debugger i see just "General."). After Resource function returned null, RenderAsync returns just "True" (or "False" - depends on value of require_for_auto_test variable) instead of "null". It's looks like scriban print result of string interpolation $"General.{string.capitalize require_for_auto_test}".

Expected behavior: at first, scriban interpolate string $"General.{string.capitalize require_for_auto_test}" and then calls function $"{resource "ru-RU" "General.True"}"

Current version of scriban library: 5.10.0

Hominis06 avatar Sep 23 '24 12:09 Hominis06

Indeed, possibly a bug. As a workaround, could you try to put the interpolation between (...)?

(Note: Too busy these days to have a look at bug fixing for this project)

xoofx avatar Sep 23 '24 15:09 xoofx

Yep, workaround with brackets works fine

Hominis06 avatar Sep 24 '24 07:09 Hominis06

Fixed by 06aed9f

xoofx avatar Mar 06 '25 15:03 xoofx

Fixed by 06aed9f

I am so happy this issue has been resolved 😀! Since string interpolation implementation is my only "major" contribution to open source software, I still find it a remarkable experience.

I sometimes thought to have a look at the code and fix the bug but I got a full-time job in the meantime and I did not have both the time and motivation to do the thing. Especially that there was a workaround that allowed using functions in interpolated strings.

Merci.

ranger-turtle avatar Mar 06 '25 16:03 ranger-turtle