# \[SOLVED\] Wanting to add a function to tpl/template\_functions.go

**URL:** <https://discourse.gohugo.io/t/solved-wanting-to-add-a-function-to-tpl-template-functions-go/4416>\
**Category:** dev\
**Created:** [October 27, 2016, 10:43pm UTC](https://discourse.gohugo.io/t/solved-wanting-to-add-a-function-to-tpl-template-functions-go/4416 "2016-10-27T22:43:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lebarde](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/lebarde/32/2207_2.png) [@lebarde](https://discourse.gohugo.io/u/lebarde)\
**Post date:** [October 27, 2016, 10:43pm UTC](https://discourse.gohugo.io/t/solved-wanting-to-add-a-function-to-tpl-template-functions-go/4416/1 "2016-10-27T22:43:54Z")

</div>

Hello,  
I am currently trying to add a function to the templating system. This one is called `widgets`. I have added it to `tpl/template_funcs.go`, and created a test site inside `examples/widgets`. But when I want to generate the test website, Hugo doesn’t recognize the `widgets` templating function:

```auto
~/dev/go/hugo$ go build
~/dev/go/hugo$ cd examples/widgets
~/dev/go/hugo/examples/widgets$ ../../hugo
Started building sites ...
ERROR: 2016/10/28 00:37:20 template.go:477: template: index.html:4: function "widgets" not defined
ERROR: 2016/10/28 00:37:20 general.go:212: Error while rendering homepage: template: "index.html" is an incomplete or empty template; defined templates are: "_internal/twitter_cards.html", "_internal/google_news.html", "_internal/shortcodes/relref.html", "_internal/shortcodes/speakerdeck.html", "_internal/_default/rss.xml", "_internal/pagination.html", "_internal/disqus.html", "_internal/google_analytics.html", "_internal/_default/robots.txt", "_internal/shortcodes/ref.html", "_internal/shortcodes/highlight.html", "_internal/shortcodes/youtube.html", "_internal/_default/sitemapindex.xml", "_internal/opengraph.html", "_internal/schema.html", "_internal/shortcodes/figure.html", "_internal/shortcodes/vimeo.html", "_internal/shortcodes/tweet.html", "_internal/_default/sitemap.xml", "_internal/shortcodes/test.html", "_internal/shortcodes/gist.html", "_internal/google_analytics_async.html"
~/dev/go/hugo/examples/widgets$ 

```

I cannot see where to search…

Everything is in [this commit](https://github.com/lebarde/hugo/commit/ac40323abbbee84c0dcb39191c7c39405dcc4d27) :

[https://github.com/lebarde/hugo/commit/ac40323abbbee84c0dcb39191c7c39405dcc4d27](https://github.com/lebarde/hugo/commit/ac40323abbbee84c0dcb39191c7c39405dcc4d27)

Thanks by advance!

---

<div class="post-metadata">

**Author:** ![moorereason](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/moorereason/32/592_2.png) [@moorereason](https://discourse.gohugo.io/u/moorereason)\
**Post date:** [October 28, 2016, 6:04pm UTC](https://discourse.gohugo.io/t/solved-wanting-to-add-a-function-to-tpl-template-functions-go/4416/2 "2016-10-28T18:04:45Z")

</div>

First, thanks for jumping in to try to solve an issue.

Quick thoughts:

1. No hugo maintainer has signed off on your proposal in [#2535](https://github.com/spf13/hugo/issues/2535). I’m all for people learning Go (yea!), but be prepared to have to redo some/all of this proposal.
2. You have syntax errors in `hugolib/widget.go`.
3. Add gofmt/goimports to your editor/IDE environment.
4. I would recommend naming the function `widget` without the trailing “s”. The “s” provides no value.
5. Much of this feature should mimic how the `partial` templates work.
6. Write tests instead of hacking in an examples folder. Simple example that works with what you have:

```diff
diff --git a/tpl/template_funcs_test.go b/tpl/template_funcs_test.go
index 16325a7..601bcab 100644
--- a/tpl/template_funcs_test.go
+++ b/tpl/template_funcs_test.go
@@ -2438,6 +2438,13 @@ func TestSHA1(t *testing.T) {
        }
 }

+func TestWidget(t *testing.T) {
+ out := widget(nil)
+ if out != template.HTML("<h1>TEST</h1>") {
+ t.Errorf("widget failed")
+ }
+}
+

```

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [October 28, 2016, 6:13pm UTC](https://discourse.gohugo.io/t/solved-wanting-to-add-a-function-to-tpl-template-functions-go/4416/3 "2016-10-28T18:13:22Z")

</div>

@lebarde my guess it is a confusion of GOPATH.

I will be short:

The Go tools expect the Go code to be in

$GOPATH/src/github.com/spf13/hugo

So if you end up in

$GOPATH/src/github.com/lebarde/hugo

There will be confusion.

So either:

1. clone or go get your fork and rename folder to spf13 to match the package imports
2. clone the original and add your fork as a remote

I use GitHub’s hub tool to do forking, and I just clone or get the original and then do a `hub fork` when I want to do some changes in a new repo – then I don’t have to think about paths. Then branching and coding … and then `hub pull-request` when I’m done. Quick as it gets.

---

<div class="post-metadata">

**Author:** ![moorereason](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/moorereason/32/592_2.png) [@moorereason](https://discourse.gohugo.io/u/moorereason)\
**Post date:** [October 28, 2016, 7:08pm UTC](https://discourse.gohugo.io/t/solved-wanting-to-add-a-function-to-tpl-template-functions-go/4416/4 "2016-10-28T19:08:40Z")

</div>

Also, see the [Using Git Remotes](https://github.com/spf13/hugo/blob/master/CONTRIBUTING.md#using-git-remotes) section of the Contributing Guide.

---

<div class="post-metadata">

**Author:** ![lebarde](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/lebarde/32/2207_2.png) [@lebarde](https://discourse.gohugo.io/u/lebarde)\
**Post date:** [October 28, 2016, 9:21pm UTC](https://discourse.gohugo.io/t/solved-wanting-to-add-a-function-to-tpl-template-functions-go/4416/5 "2016-10-28T21:21:12Z")

</div>

Thanks for your answers. I appreciate much.

@moorereason:

1. I will read again the contributing guide. All to say that I agree completely about a possible rearrange/move to thrash bin (I won’t be offended at all if a pull request does not succeed, if ever I can develop something).  
As I do not know about the reviewing/proposal process, I wanted to dive into the widget problem to let the discussion go on (I hadn’t much answers about my proposal so let’s see what it could become !). I still won’t be offended if somebody implemented the whole thing before me, as I would only like a widget system to appear 🙂
2. Actually `widgets.go` was included in the commit but I did nothing on it. The whole commit was made fastly to begin diving into.
3. Thanks!
4. This is related to the [Widget mechanism](https://discuss.gohugo.io/t/widget-mechanism/4428) thread.
5. Totally agreeing with you. IMO a widget mechanism is narrower than partials in terms of possibilities (widgets are intended for HTML, which whereas partials are agnostic). See the [Widget mechanism](https://discuss.gohugo.io/t/widget-mechanism/4428) thread.
6. This was my very first draft to explore. Thanks for the test.

@bep I will look at the gopath. Thanks.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [October 28, 2016, 9:26pm UTC](https://discourse.gohugo.io/t/solved-wanting-to-add-a-function-to-tpl-template-functions-go/4416/6 "2016-10-28T21:26:39Z")

</div>

Can you take the Widget discussion in another thread?

---

<div class="post-metadata">

**Author:** ![lebarde](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/lebarde/32/2207_2.png) [@lebarde](https://discourse.gohugo.io/u/lebarde)\
**Post date:** [October 28, 2016, 9:42pm UTC](https://discourse.gohugo.io/t/solved-wanting-to-add-a-function-to-tpl-template-functions-go/4416/7 "2016-10-28T21:42:31Z")

</div>

Okay. Here is the thread i opened for the widget’s discussion: [Widget mechanism](https://discuss.gohugo.io/t/widget-mechanism/4428).

---

<div class="post-metadata">

**Author:** ![lebarde](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/lebarde/32/2207_2.png) [@lebarde](https://discourse.gohugo.io/u/lebarde)\
**Post date:** [October 30, 2016, 9:53pm UTC](https://discourse.gohugo.io/t/solved-wanting-to-add-a-function-to-tpl-template-functions-go/4416/8 "2016-10-30T21:53:01Z")

</div>

Okay thanks @bep for your `$GOPATH` remark. This is it. Actually I had my forked repository somewhere else, but `$GOPATH/src/github.com/spf13/hugo` was well cloned from the main source. I made it with symlinks:

```auto
~/dev/go/hugo/$ echo $GOPATH
/my-user-home/dev/go
~/dev/go/hugo/$ rm -r ~/dev/go/src/github.com/spf13/hugo/
~/dev/go/hugo/$ ln -s ~/dev/go/hugo/ ~/dev/go/src/github.com/spf13/hugo

```

And then:

```auto
~/dev/go/hugo$ go build
# github.com/spf13/hugo/hugolib 
../src/github.com/spf13/hugo/hugolib/widget.go:25: syntax error: non-declaration statement outside function body

```

Now I can start working!
