Delivering default data to templates - Sharykhin/go-modular GitHub Wiki

There is an opportunity to set default data for all templates if it's neccessary.

There are two methods which are responsible for shownig templates in the BaseController: Render and RenderView.

Both of them create an ability to use global data in the templates.

import global "go-modular/application/config/global"

                ...
 
err = t.Execute(res,struct {
		  Data interface{}
          App  global.GlolbalData	
		}{
		  Data: data,
          App: global.App,
		})

So App contains data from global package App struct. Open this package:

package global


type GlolbalData struct {
	Properties map[string]interface{}
}

var App GlolbalData

func init() {	
	App.Properties = map[string]interface{}{
		 "ApplicationName": "GO-MODULAR",
		}
}

As you can see, here we initialize instance of GlobalData stuct and we have Properties with data, which will be available in any templates

In the template you can get data from App:

<h2>App name: <i>{{.App.Properties.ApplicationName}}</i></h2>

So go {{.App.Properties.PropertyName}} give us the value of current key(PropertyName)

⚠️ **GitHub.com Fallback** ⚠️