Add Param(key) to Node and Page
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 12 Oct 2015 03:51:04 +0000 (05:51 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 12 Oct 2015 04:11:01 +0000 (06:11 +0200)
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 #1462

hugolib/node.go
hugolib/page.go

index f8f8df59426428890a9fa1333faed06b0a524bb7..dcbf0d146a6a17329d0dd2fa824b9f9109fda256 100644 (file)
@@ -14,6 +14,7 @@
 package hugolib
 
 import (
+       "github.com/spf13/cast"
        "html/template"
        "sync"
        "time"
@@ -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
 }
index e08e764af955baeee5ea2a69c5b789114088eeac..2979822c3bd0add00db4226c983ffd194713bffe 100644 (file)
@@ -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()