Programming/golang2024. 2. 16. 11:44

아래의 붉은 부분에 대한 해석이 필요해서 검색

func main() {
  // Echo instance
  e := echo.New()

  // Instantiate a template registry with an array of template set
  // Ref: https://gist.github.com/rand99/808e6e9702c00ce64803d94abff65678
  templates := make(map[string]*template.Template)
  templates["home.html"] = template.Must(template.ParseFiles("view/home.html", "view/base.html"))
  templates["about.html"] = template.Must(template.ParseFiles("view/about.html", "view/base.html"))
  e.Renderer = &TemplateRegistry{
    templates: templates,
  }

  // Route => handler
  e.GET("/", handler.HomeHandler)
  e.GET("/about", handler.AboutHandler)

  // Start the Echo server
  e.Logger.Fatal(e.Start(":1323"))
}

[링크 : https://gitlab.com/ykyuen/golang-echo-template-example/-/blob/master/main.go?ref_type=heads]

[링크 : https://gitlab.com/ykyuen/golang-echo-template-example]

[링크 : https://dev.to/ykyuen/setup-nested-html-template-in-go-echo-web-framework-e9b]

 

ParseFiles() 는 가장 마지막 파일이 결과로 나온다.

다르게 해석하면 앞에서 부터 웹 레이아웃 구성요소들로 넣고, 가장 마지막에 페이지 하나에 대한 템플릿을 두면 된다.

func (*Template) ParseFiles ¶
func (t *Template) ParseFiles(filenames ...string) (*Template, error)
ParseFiles parses the named files and associates the resulting templates with t. If an error occurs, parsing stops and the returned template is nil; otherwise it is t. There must be at least one file.

When parsing multiple files with the same name in different directories, the last one mentioned will be the one that results.

ParseFiles returns an error if t or any associated template has already been executed.

[링크 : https://pkg.go.dev/html/template#Template.ParseFiles]

'Programming > golang' 카테고리의 다른 글

golang echo i18n  (0) 2024.02.19
golang package  (0) 2024.02.19
golang runtime.GOMAXPROCS()  (0) 2024.02.15
golang echo 템플릿 파일로 불러오기  (0) 2024.02.14
golang switch  (0) 2024.02.08
Posted by 구차니