Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a widget mechanism to Hugo #3412

Closed
wants to merge 2 commits into from
Closed

Commits on May 2, 2017

  1. Add a widget mechanism to Hugo

    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
    lebarde committed May 2, 2017
    Configuration menu
    Copy the full SHA
    fc7c44a View commit details
    Browse the repository at this point in the history

Commits on May 3, 2017

  1. tpl/widgets: Clarify widgets' environment variables

    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.
    lebarde committed May 3, 2017
    Configuration menu
    Copy the full SHA
    0706ecf View commit details
    Browse the repository at this point in the history