[SOLVED] Wanting to add a function to tpl/template_functions.go

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:

~/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

Thanks by advance!

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. 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 --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")
+       }
+}
+
1 Like

@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.

2 Likes

Also, see the Using Git Remotes section of the Contributing Guide.

1 Like

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 :slight_smile:
  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 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 thread.
  6. This was my very first draft to explore. Thanks for the test.

@bep I will look at the gopath. Thanks.

Can you take the Widget discussion in another thread?

Okay. Here is the thread i opened for the widget’s discussion: Widget mechanism.

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:

~/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:

~/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!