Hugo: Passing extra information to a partial template instantiation

Created on 27 Aug 2014  路  8Comments  路  Source: gohugoio/hugo

I'd like to know whether there's any best practice about passing extra arguments to a partial template. Let's assume I have a partial template like this:

<button class='btn btn-default btn-sm' type='button'>
<span class='glyphicon glyphicon-chevron-right'></span>
</button>

I'd like to be able to use a single template to produce a button with glyphicon-chevron-right and glyphicon-chevron-left. However, as far as I can tell:

  • I can't create complex variables in templates (ie. a map containing current . and direction key);
  • I can't pass more than one argument to {{ template "foo" bar }} call;
  • I can't add new keys to ..

Any other way to get this functionality? Currently I'm simply defining two separate templates, but that's clumsy.

Enhancement

Most helpful comment

0.15 introduces a map that can be used for this https://github.com/spf13/hugo/pull/1463
See dict function http://gohugo.io/templates/functions/#dict

All 8 comments

I understand what you are asking to do. Not sure what the best solution is for it today. Would need to think about a good solution.

Any update on this?

Is it possible for to declare a map as a variable and pass that?

Ran into this issue today as well - wasn't sure if my understanding of Go template was bad, or if there's no good way. I would have spent less time getting stuck if documentation mentioned that there's no good way. I was trying to migrate a Jekyll theme, and Jekyll has a way to pass multiple parameters to its "includes" call.

It seems the best solution so far has been the .Scratch feature that @bep recently added.

@spf13 wow, thank you for a quick reply! @bep that's great!

For everyone else, here's the relevant pull request:
https://github.com/spf13/hugo/pull/746

Hopefully the documentation page reflects the change soon:
http://gohugo.io/extras/scratch

I don't think we'll gonna provide anything "smarter" than Scratch anything soon, unless provided by the Go templates -- so close this. Hate open issues :-)

Oh, and until 0.13 gets out in the open -- the latest docs can be read nicely at https://github.com/spf13/hugo/blob/master/docs/content/extras/scratch.md

0.15 introduces a map that can be used for this https://github.com/spf13/hugo/pull/1463
See dict function http://gohugo.io/templates/functions/#dict

Was this page helpful?
0 / 5 - 0 ratings