Skip to content

Commit

Permalink
Add Param(key) to Node and Page
Browse files Browse the repository at this point in the history
This  is a convenience method to do lookups in Page's (Page only)  and Site's Params map (Page and Node), in that order.

Fixes gohugoio#1462
  • Loading branch information
bep authored and bramp committed Dec 17, 2015
1 parent 06d45be commit cd7b9c8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions hugolib/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package hugolib

import (
"github.com/spf13/cast"
"html/template"
"sync"
"time"
Expand Down Expand Up @@ -86,6 +87,17 @@ func (n *Node) IsMenuCurrent(menuID string, inme *MenuEntry) bool {
return false
}

// Param is a convenience method to do lookups in Site's Params map.
//
// This method is also implemented on Page.
func (n *Node) Param(key interface{}) (interface{}, error) {
keyStr, err := cast.ToStringE(key)
if err != nil {
return nil, err
}
return n.Site.Params[keyStr], err
}

func (n *Node) Hugo() *HugoInfo {
return hugoInfo
}
Expand Down
15 changes: 15 additions & 0 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ func (p *Page) IsPage() bool {
return true
}

// Param is a convenience method to do lookups in Page's and Site's Params map,
// in that order.
//
// This method is also implemented on Node.
func (p *Page) Param(key interface{}) (interface{}, error) {
keyStr, err := cast.ToStringE(key)
if err != nil {
return nil, err
}
if val, ok := p.Params[keyStr]; ok {
return val, nil
}
return p.Site.Params[keyStr], nil
}

func (p *Page) Author() Author {
authors := p.Authors()

Expand Down

0 comments on commit cd7b9c8

Please sign in to comment.