Including other templates - Sharykhin/go-modular GitHub Wiki

Methods of BaseController Render and RenderView have third parameter ([]stirng), which is responsible for additional templates

Let's look on an example:

if err := ctrl.Render(res, "other", []string{"include","modules/admin:appointments"}, struct {		
		Article string		
		Appointments int
	}{		
		Article: "Make a join",		
		Appointments: 188,
	}); err != nil {
		return err
	}

As you can see, in the third parametes there were used slice of strings (include,modules/admin:appointments). According to these names BaseController tries to find files in the appropriates directories (e.x. application/views/include.html, and application/modules/views/appointments.html)

To make it working correctly we have to include names of defined template into our main template, according to our example in other.html:

   ...
   {{ template "ajax" }}
   {{ template "appointments" .Data }}
   ...

And finally, each included template has to be defined by using "define" function include.html:

{{define "ajax"}}
	<h4>I am included template2</h4>	
{{end}}

appointments.html:

{{define "appointments"}}
	<h4>There are {{.Appointments}} appointments</h4>	
{{end}}

As you can notice there is an ability to give a data, whcih might be used in the included template

If you don't want to add included files, just use nil in the third parameter

ctrl.RenderView(res, "post", nil, struct {User  string
		Dates [2]int
	}{
		User:  "John",
		Dates: [2]int{2, 3},
	})
⚠️ **GitHub.com Fallback** ⚠️