From c2f2c344c20ec2fe4d11250d7080550418e9061c Mon Sep 17 00:00:00 2001 From: surdeus Date: Wed, 26 Jun 2024 20:29:38 +0500 Subject: [PATCH] ... --- tengo.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/tengo.go b/tengo.go index 66df5dd..c1ea7fd 100644 --- a/tengo.go +++ b/tengo.go @@ -63,11 +63,12 @@ func (pp *Tengo) Eval( const retHead = ` context := {} pp := immutable({ - filepath: __pp_filepath__, + fpath: __pp_filepath__, printf : __pp_printf__, print : __pp_print__, println : __pp_println__, - write_raw : __pp_write_raw__ + write_raw : __pp_write_raw__, + include: __pp_include__ }) ` @@ -116,6 +117,27 @@ func (pp *Tengo) Eval( pp.preCompile(ctx, script) } + script.Add("__pp_include__", &tengo.UserFunction{ + Value: func(args ...tengo.Object) (tengo.Object, error){ + if len(args) < 1 { + return nil, tengo.ErrWrongNumArguments + } + format, ok := tengo.ToString(args[0]) + if !ok { + return nil, tengo.ErrInvalidArgumentType{ + Expected: "string", + } + } + gargs := make([]any, len(args) - 1) + for i := range gargs { + gargs[i] = tengo.ToInterface(args[i+1]) + //fmt.Printf("shit: %q\n", gargs[i]) + } + fmt.Fprintf(&retBuf, format, gargs...) + return nil, nil + //return tengo.FromInterface([]byte(str)) + }, + }) script.Add("__pp_filepath__", filePath) script.Add("__pp_printf__", &tengo.UserFunction{ Value: func(args ...tengo.Object) (tengo.Object, error){