forked from gohugoio/hugo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Here is a first attempt to get widgets into Hugo. General tests are ok on my dev environment, but I have not already written tests for widgets. 1. Create a site 2. Create a widget directory inside `/widgets`. It should look like this: ``` widgets/ └── text ├── layouts │ └── widget.html └── README.md ``` Note that the name `widget.html` is mandatory. *Currently the context is the content of the config parameter `widgets.[mywidgetarea].[mywidget].options`*. Variables are accessible with `.content` for a text widget like the following and as described in the config below. ``` {{- if isset . "content" -}} {{- .content | safeHTML -}} {{- else -}}<pre>Here is a text widget, but there is nothing to print. Please define options.content inside every text widget in your config.</pre> {{- end -}} ``` 3. Configure your site with a `widgets` variable describing widgets inside widget areas: ``` widgets: sidebar: - type: text options: content: "<h1>IT WORKS from config</h1>" parser: html showcase: - type: text options: content: "Here lies a showcase." footer: - type: text options: content: "Powered by Hugo with widgets." foo: bar ``` 4. Create a template using the `widgets` call. This can be done like this: `{{ widgets "sidebar" . }}`. 5. Create content. You can also use the widget's shortcode: `{{% widgets "showcase" %}}` 6. Build and enjoy. - Currently the widgets' context is only the content of the config variable. We should add a wider context (easy). - I have not studied the impact on performances. - Else? Fixes gohugoio#2683 See gohugoio#2535
- Loading branch information
Showing
12 changed files
with
358 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// Copyright 2016-present The Hugo Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package hugolib | ||
|
||
import ( | ||
"html/template" | ||
|
||
"github.com/spf13/cast" | ||
"github.com/gohugoio/hugo/tpl" | ||
) | ||
|
||
// TODO See the shortcode system to see the structure | ||
|
||
// Data structures and methods | ||
// =========================== | ||
|
||
// A WidgetEntry represents a widget item defined | ||
// in the site config. | ||
// (TODO: See hugolib/menu.go for the data structure) | ||
type Widget struct { | ||
Type string | ||
Params map[string]interface{} | ||
Identifier string | ||
Weight int | ||
Template *template.Template | ||
} | ||
|
||
func newWidget(widgetType string, options interface{}) (*Widget, error) { | ||
return &Widget{Type: widgetType, Params: options.(map[string]interface{})}, nil | ||
} | ||
|
||
type WidgetArea struct { | ||
Name string | ||
Widgets []*Widget | ||
Template *template.Template | ||
} | ||
|
||
func newWidgetArea(waname string) *WidgetArea { | ||
// TODO ?? | ||
return &WidgetArea{Name: waname, Widgets: nil, Template: nil} | ||
} | ||
|
||
type Widgets map[string]*WidgetArea | ||
|
||
// Internal widgets building | ||
// ========================= | ||
|
||
// WidgetsConfig parses the widgets config variable | ||
// (is a collection) and calls every widget configuration. | ||
func (s *Site) getWidgetsFromConfig() Widgets { | ||
ret := Widgets{} | ||
|
||
if conf := s.Cfg.GetStringMap("widgets"); conf != nil { | ||
for waname, widgetarea := range conf { | ||
// wa is a widget area defined in the conf file | ||
wa, err := cast.ToSliceE(widgetarea) | ||
if err != nil { | ||
s.Log.ERROR.Printf("unable to process widgets in site config\n") | ||
s.Log.ERROR.Println(err) | ||
} | ||
|
||
// Instantiate a WidgetArea | ||
waobj := newWidgetArea(waname) | ||
|
||
// Retrieve all widgets | ||
for _, w := range wa { | ||
iw, err := cast.ToStringMapE(w) | ||
|
||
if err != nil { | ||
s.Log.ERROR.Printf("unable to process widget inside widget area in site config\n") | ||
s.Log.ERROR.Println(err) | ||
} | ||
|
||
// iw represents a widget inside a widget area | ||
wtype := cast.ToString(iw["type"]) | ||
woptions, err := cast.ToStringMapE(iw["options"]) | ||
wobj, err := newWidget(wtype, woptions) | ||
|
||
if err != nil { | ||
s.Log.ERROR.Printf("unable to instantiate widget: %s\n", iw) | ||
s.Log.ERROR.Println(err) | ||
} | ||
|
||
// then append it to the widget area object | ||
waobj.Widgets = append(waobj.Widgets, wobj) | ||
} | ||
|
||
// don't forget to append that widget area to the | ||
// Widgets object | ||
ret[waname] = waobj | ||
} | ||
} | ||
|
||
return ret | ||
} | ||
|
||
// instantiateWidget retrieves the widget's files | ||
// and creates the templates | ||
func (s *Site) instantiateWidget(temp tpl.TemplateHandler, wa *WidgetArea, w *Widget) *Widget { | ||
// Load this widget's templates | ||
// using the site object's owner.tmpl | ||
temp.LoadTemplates(s.PathSpec.GetWidgetsDirPath()+"/"+w.Type+"/layouts", "widgets/"+w.Type) | ||
|
||
return w | ||
} | ||
|
||
// Main widgets entry point | ||
// ======================== | ||
|
||
// This function adds the whole widgets' template code | ||
// in the Site object. This is of type template.HTML. | ||
// This function is called from hugo_sites. | ||
func (s *Site) injectWidgets(temp tpl.TemplateHandler) error { | ||
// Get widgets. This gives all information we need but | ||
// does not already read widget files. | ||
widgets := s.getWidgetsFromConfig() | ||
|
||
for _, widgetarea := range widgets { | ||
// _ is waname, if ever we need | ||
|
||
for _, w := range widgetarea.Widgets { | ||
w = s.instantiateWidget(temp, widgetarea, w) | ||
} | ||
} | ||
|
||
// We now have all widgets with their templates. | ||
// Generate all widget areas with their templates | ||
// Now the template's content will be used inside | ||
// the main template files inside tpl/template_funcs | ||
// and in the templates using {{ widgets "mywidgetarea" }} | ||
s.Widgets = widgets | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.