render

package
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Render

type Render struct {
	Renderer   string
	RootPath   string
	Secure     bool
	Port       string
	ServerName string
	JetViews   *jet.Set
	Session    *scs.SessionManager
}

func (*Render) GoPage

func (g *Render) GoPage(w http.ResponseWriter, r *http.Request, view string, data interface{}) error

GoPage renders a standard Go template

func (*Render) JetPage

func (g *Render) JetPage(w http.ResponseWriter, r *http.Request, templateName string, variables, data interface{}) error

JetPage renders a template using the jet templating language

func (*Render) Page

func (g *Render) Page(w http.ResponseWriter, r *http.Request, view string, variables, data interface{}) error

type TemplateData

type TemplateData struct {
	IsAuthenticated bool                   // True if user is logged in (userID in session)
	IntMap          map[string]int         // Integer values to pass to template
	StringMap       map[string]string      // String values to pass to template
	FloatMap        map[string]float32     // Float values to pass to template
	Data            map[string]interface{} // Any other data to pass to template
	CSRFToken       string                 // CSRF protection token for forms
	Port            string                 // Server port
	ServerName      string                 // Server name
	Secure          bool                   // True if HTTPS
	Error           string                 // One-time error message (from session)
	Flash           string                 // One-time flash message (from session)
}

TemplateData holds data that is automatically passed to all templates. These variables are available in your Jet or Go templates:

Automatically Populated (via defaultData):

  • CSRFToken: CSRF protection token for forms. Use in forms like: <input type="hidden" name="csrf_token" value="{{.CSRFToken}}">
  • Flash: One-time flash message from session (auto-removed after display)
  • Error: One-time error message from session (auto-removed after display)
  • IsAuthenticated: True if "userID" exists in session
  • Secure: Whether the connection is HTTPS
  • ServerName: The server name from configuration
  • Port: The port the server is running on

User-Provided Data Maps:

  • IntMap: For passing integer values to templates
  • StringMap: For passing string values to templates
  • FloatMap: For passing float values to templates
  • Data: For passing any other data to templates

Example usage in handler:

td := &render.TemplateData{
    StringMap: map[string]string{"title": "My Page"},
    Data: map[string]interface{}{"users": users},
}
h.render(w, r, "users", nil, td)

Example usage in template (Jet):

<h1>{{ .StringMap.title }}</h1>
{{ if .Flash }}<div class="alert">{{ .Flash }}</div>{{ end }}
{{ if .Error }}<div class="error">{{ .Error }}</div>{{ end }}
<form>
    <input type="hidden" name="csrf_token" value="{{ .CSRFToken }}">
</form>

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL