0

I’ve created a template which works as expected, now when the template is become bigger I want to Divide it to several files, what I did is the following for example

bigfile.txt :)

echo “I’m pre process”

echo “process”

echo “I’m post process”




t := template.Must(template.New("bigfile.txt").Parse(bigfile.txt))

Now this is working without any problem.

What I did now is divide the big file to 3 files

**base_pre.txt**

And here is the code inside

{{define "base_pre”}}

echo “I’m pre process”

{{end}}

base_pre.txt

And here is the code inside

{{define "base_post”}}

echo “I’m post process”

{{end}}

And in thebigfile.txtI’ve changed the

{{template "base_pre"}}

echo process

{{template "base_post”}}

When I run it I got error

ERROR template: bigfile.txt:1:11: executing “bigfile.txt" at <{{template "base_pre...>: template "base_pre" not defined

07_05_GuyT
  • 2,787
  • 14
  • 43
  • 88
  • 1
    First the last line in bigfile.txt should be {{template "base_post"}}, second: did you load the template files base_pre.txt and base_post.txt? – Vincenzo Maggio Aug 27 '18 at 09:14
  • https://golang.org/pkg/html/template/#Template.ParseFiles – mkopriva Aug 27 '18 at 09:16
  • @VincenzoMaggio - sorry i've fixed the typo, it's `template` as you mention (same error exist... how should I load them ? i've a place `template.New("bigfile.txt")` to load only one template ... – 07_05_GuyT Aug 27 '18 at 10:11
  • Sorry no time to answer right now, please refer to https://hackernoon.com/golang-template-2-template-composition-and-how-to-organize-template-files-4cb40bcdf8f6 to have some info – Vincenzo Maggio Aug 27 '18 at 10:16
  • See an example of this here: [The Go Template ParseFiles func parsing multiple files](https://stackoverflow.com/questions/44979276/the-go-template-parsefiles-func-parsing-multiple-files/44979550#44979550). – icza Aug 27 '18 at 10:45

0 Answers0