shithub: hugo

Download patch

ref: 078fad49e2508759aa33995eb0a4464ce0a63595
parent: 3586d77bd57ceb45e23892a552be616dac0cf7b4
author: Bjørn Erik Pedersen <[email protected]>
date: Mon Oct 12 01:51:04 EDT 2015

Add Param(key) to Node and Page

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

--- a/hugolib/node.go
+++ b/hugolib/node.go
@@ -14,6 +14,7 @@
 package hugolib
 
 import (
+	"github.com/spf13/cast"
 	"html/template"
 	"sync"
 	"time"
@@ -84,6 +85,17 @@
 	}
 
 	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 {
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -132,6 +132,21 @@
 	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()