Paste template in to main template

Hi.
How paste include template in main template?

tpl = template.Must(template.ParseGlob(“/*.tpl”))

Function ParseGlob select to 10 files…
For example: “statistic.tpl”, “character.tpl”, “forum.tpl”, “news.tpl”, “newsfull.tpl”, “server.tpl”…

Thank :wink:

Hey @KoHCyJI,

To include HTML into a template, you’ll need to use the template.HTML field.

https://golang.org/pkg/html/template/#HTML

Here’s a small example I just wrote for you to check out:

package main

import (
	"html/template"
	"log"
	"os"
)

var t = template.Must(template.New("t").Parse("{{.Content}}"))

func main() {
	data := struct {
		Content template.HTML
	}{
		"<a href='#link'>Link</a>",
	}

	if err := t.Execute(os.Stdout, data); err != nil {
		log.Fatalln(err)
	}
}

The above will output the following: <a href='#link'>Link</a>
However if Content is set to a string type, the output will be: &lt;a href=&#39;#link&#39;&gt;Link&lt;/a&gt;

Edit: If you were actually referring to embedding templates within each other (for example a header or footer), since I realize I may have misinterpreted your question, check out this example here: https://github.com/radovskyb/go-packages/tree/master/html/template/parsefiles

data := struct { Content template.HTML }{ "<a href='#link'>Link</a>", }
Google translate :smiling_imp:
How can I use one of the ParseGlob function templates instead of the (< a href=’#link’>Link< /a>) code?
I want to insert the selected one of the templates into the main template.

An example for a greater understanding of me

data := struct { Content template.HTML }{ "news.tpl", // How do I specify the template file to be inserted here? }
Thanks for the help :wink:

Hey @KoHCyJI,

I realized after replying to you that I’d misinterpreted your question.

Check out my edited answer that points you to what you’re looking for here: https://github.com/radovskyb/go-packages/tree/master/html/template/parsefiles

Here’s a really small example that’s similar to the above link:

main.go:

package main

import (
	"html/template"
	"log"
	"os"
)

var t = template.Must(template.New("index.tmpl").ParseFiles(
	"index.tmpl",
	"content.tmpl",
))

func main() {
	if err := t.Execute(os.Stdout, nil); err != nil {
		log.Fatalln(err)
	}
}

index.tmpl:

Main part of template
{{ template "content" }}

content.tmpl:

{{ define "content" }}
Content Goes Here
{{ end }}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.