scriban icon indicating copy to clipboard operation
scriban copied to clipboard

Named arguments with include

Open jimmytsoares opened this issue 6 years ago • 3 comments

Hello! I'm working with Scriban for the last couple weeks and I'm trying to learn almost everything possible to do with Scriban.

Today I stumbled across something that's being hard to figure out. Is it possible have named arguments with the Include function? I'm trying something like this: {{ include 'test.html' teststring: 'abc'}} And suppose 'test.html' is: <h1>{{$teststring}}</h1> I would expect the output to be: <h1>abc</h1> However I'm only getting blank results. When I use: <h1>{{$1}}</h1> I get the expected results, so that me wonder if I can have or not named arguments with the include funtion. But then when I just tried printing {{$}} I got [, teststring: abc], making me think that it is possible since the information is in the $ array.

I've already tried the following to get the teststring argument: {{$teststring}},{{$[teststring]}},{{$.teststring}}

Thanks for this awesome templating engine!

jimmytsoares avatar May 31 '19 21:05 jimmytsoares

For scriban versions that still output the full arguments when you print with {{$}} you can use the following workaround: Include file (myinclude):

{{
    func getParamValue 
       for param in $0
            if param
                if param.name == $1
                    param.value
                end
            end
        end
    end
}}
{{
    getParamValue $ "test"
}}

Template file: {{ include 'myinclude' test:'myvalue'}} This will output myvalue as requested.

In later versions of Sriban however, it looks like the named arguments arent passed but only the values are.

mwillebrands avatar Feb 13 '21 19:02 mwillebrands

This {{$.teststring}} should have worked, but there was a bug that I just fixed by commit c443698ada2938e4d8b7a0ab9acac3d2c0aa60cd and should be part of upcoming Scriban 3.5.0

We could change to {{$teststring}} only for a 4.x version of Scriban in the future.

In the meantime with 3.5.0, you could issue a with $ in the function and you would have access to {{teststring}} directly

xoofx avatar Feb 13 '21 20:02 xoofx