Skip to content

Array in yml file as context is not supported #328

Description

@boatrainlsz

I have a context.yml file contains:
ARRAY: ['elem1', 'elem2', 'elem3']
template.conf file contains:
array: {{ ARRAY }}

I did templating and expected output is:
array: ['elem1', 'elem2', 'elem3']
and actual output is:
array: <[]interface {} Value>

code snippet:

package main

import (
	"fmt"
	"github.com/flosch/pongo2/v6"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"os"
)

func main() {
	//write ARRAY: ['elem1', 'elem2', 'elem3'] to context.yml
	os.WriteFile("context.yml", []byte("ARRAY: ['elem1', 'elem2', 'elem3']"), 0644)
	//write array: {{ ARRAY }} to template.conf
	os.WriteFile("template.conf", []byte("array: {{ ARRAY }}"), 0644)
	readByte, err := readFile("template.conf")
	if err != nil {
		return
	}
	context := pongo2.Context{}
	contextFile, err := os.Open("context.yml")
	if err != nil {
		return
	}
	out := make(map[string]interface{})
	if err := yaml.NewDecoder(contextFile).Decode(out); err != nil {
		return
	}
	fmt.Println("yml decode: ", out)
	contextFile.Seek(0, 0)
	if err := yaml.NewDecoder(contextFile).Decode(&context); err != nil {
		return
	}
	temp, err := pongo2.FromBytes(readByte)
	if err != nil {
		return
	}
	output, err := temp.Execute(context)
	if err != nil {
		return
	}
	fmt.Println(output)
}
func readFile(path string) ([]byte, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer file.Close()
	byteValue, err := ioutil.ReadAll(file)
	return byteValue, nil

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions