Skip to content

Commit

Permalink
tpl/widgets: Clarify widgets' environment variables
Browse files Browse the repository at this point in the history
The context inside widget templates is now:

- `.` can access all classical site variables (normally including `.Content` and so on). `.Content` refers to the actual page content content.
- `.Widget` is defined as follows:
```
type Widget struct {
	Type       string
	Params     map[string]interface{}
	Identifier string
	Weight     int
	Template   *template.Template
}
```
In particular, you can access `.Widget.Type` (name of widget type) and `.Widget.Params` (config parameters).
- `.WidgetArea` contains the widget collection in the current widget area.
- `.Site.Widgets` contains the collection of all widget areas.
  • Loading branch information
lebarde committed May 3, 2017
1 parent fc7c44a commit 0706ecf
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion tpl/tplimpl/template_embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ func (t *templateHandler) embedTemplates() {
{{- if eq .Name $._wa -}}{{/* Display only the current widget area */}}
<div class="widget-area widget-area-{{ .Name }}">
{{- $waname := .Name -}}
{{- $wa := . -}}
{{ range .Widgets -}}
<div class="widget widget-{{ .Type }}">
{{ $context := (dict "$" $.c "wa" $._wa "w" .Type "Params" .Params) }}
{{ $context := (dict "$" $.c "WidgetArea" $wa "Widget" .) }}
{{ partial (print .Type "/widget.html") "widgets" $context }}
</div>
{{- end }}{{/* end range widgets */}}
Expand Down
6 changes: 3 additions & 3 deletions tpl/widget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"html/template"

"github.com/spf13/cast"
bp "github.com/spf13/hugo/bufferpool"
"github.com/spf13/hugo/deps"
)
Expand All @@ -39,9 +40,8 @@ type Namespace struct {
func (ns *Namespace) Widgets(name string, context interface{}) (interface{}, error) {
// Add (_wa: name) index/value to context to access it inside
// the embedded template (as Widget Area)
outcontext := make(map[string]interface{})
outcontext["c"] = context
outcontext["_wa"] = name
outcontext := cast.ToStringMap(context)
outcontext["_wa"] = name // The widget area name

// See in template_embedded for widgets.html
templ := ns.deps.Tmpl.Lookup("_internal/widgets.html")
Expand Down

0 comments on commit 0706ecf

Please sign in to comment.