Skip to content

Embed structs seems not working #4

@syrm

Description

@syrm

Hello

I have a problem when i'm using embed structs. Let me explain.
I have this code :

type TemplateReader struct {
	filename string
}

func (t TemplateReader) TemplateText() string {
	fileContent, err := os.ReadFile(t.filename)

	if err != nil {
		return "template not found: " + t.filename
	}

	return string(fileContent)
}

type Layout struct {
	Class string
	Body  Test `tmpl:"body"`

	TemplateReader
}

type Test struct {
}

func (*Test) TemplateText() string {
	fileContent, err := os.ReadFile("feature/authentication/login/template/login.html")

	if err != nil {
		return "template not found: login"
	}

	return string(fileContent)
}

t := &Test{}
body := &Layout{
    "main-register",
    *t,
    TemplateReader{
	filename: "template/base.html",
    },
}

ta := tmpl.MustCompile(body, compilerOption)
result, err := ta.RenderToString(body)

With this code, the template is not found, its output template not found: without the template name.

When i'm calling directly fmt.Printf("%+v\n", body.TemplateText()) the template is found.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions