It looks like Hugo doesn’t currently support passing more than one parameter to templates. Is this true? You see, I’ve been using the following template for a navigation bar:
<ul>
{{ range . }}
<li>
<a href="{{ .Url }}">{{ .Name }}</a>
{{ if .HasChildren }}
{{ partial "nav" .Children }}
{{ end }}
</li>
{{ end }}
</ul>
I pass .Site.Menus.main into this partial from the main template.
But when I try to add support for checking “is this the current page”, I need access to .IsMenuCurrent, which is part of the page rather than the *hugolib.MenuEntry. So, what I’d like to do is pass in both the current menu and the top-level page object:
{{ partial "nav" .Site.Menus.main . }}
Unlike shortcodes, however, there doesn’t seem to be a mechanism to use multiple parameters (and, as far as I can tell, I can’t call a shortcake from inside a template).
So… how would you solve this problem? Could support for multiple parameters be added to templates? Is there a better way of doing this?
Also, even if you are in an inner context, e.g. inside a range or with, you may always access .Site.Menus.main by adding $ before it, i.e.:
$.Site.Menus.main
So you do not need to pass it into a partial.
I forgot where I learned this trick, but it enabled me to make my earlier change to the the internal rss.xml template less redundant in the following patch:
Don’t worry, I learned it only 36 hours ago, and I can’t even remember where I got that information. Let me go search… (Searched for like 5 minutes…) Ah! There it is!
When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.
but, without an example, and without an explanation on how it is used, it is totally not obvious to me and I would have totally missed it or just plain don’t get it. :-p
The issue with this is that it only lets me reference the thing that was passed into the template (in my case, a *hugolib.Menu). This doesn’t help me get back to the Page object, just back to the Menu that I’m currently iterating over.
I will investigate the Scratch mechanism to see if it can help me…
Ok, it doesn’t look like Scratch will help me: only Node objects have a Scratch, so there’s no way to correctly handle recursive navigation partials: I’d have to overwrite a shared Scratch while rendering /foo in order to render /foo/a, which would preclude me from then correctly rendering /foo/b.
So, it looks like I really, really need to be able to pass multiple arguments to a partial template if this is even going to work correctly.
I ran into this problem just now. I had two partials that need .Site.BaseURL, but only have access to a custom Params object (one from .Site.Params.Header and other from .Site.Params.Cells.List). Refactoring to make this accessible is a bit frustrating, but I guess it’s the only option for now. I’d love to have arguments in the same manner as shortcodes.
I think the absUrl function should handle rewriting the url with the .Site.BaseURL , but yes it was one of my original motivations for adding the dict function.