Hugo: Add `target` attribute to menu items

Created on 16 Jun 2017  路  10Comments  路  Source: gohugoio/hugo

I'd really like Hugo menus to support the target attributes natively in menus, so links can open in new windows. I've had many use-cases for this in non-Hugo project, and it's only a matter of time before I need to also do this within Hugo. As far as I can work out, there is no way to do this currently within menus without some kind of hack with Site Params, or data files.

I've had a quick look though hugolib/menu.go and it doesn't seem like it should be too difficult to implement. But I have next to no experience with Go so could be totally wrong :)

If this is something that would benefit the Hugo project, I'd be happy to take a stab at the code in the next week or so. It seems like a good newbie commit ;)

What do you all think?

Stale

Most helpful comment

The slight problem with this is that Hugo doesn't produce the href HTML constructs, it just supplies an URL and some title, and adding Target seems a little arbitrary and very tightly coupled to a concern that Hugo really does not care too much about (how does this work in ebook formats' menus?).

So, a better approach would maybe be to follow the .Params pattern we have elsewhere, so one could do:

$target := .Menu.Params.target

Or even maybe also on the menu item level.

Just a thought.

/cc @digitalcraftsman

All 10 comments

What do you all think?

I agree, but I'm pretty sure we have an open PR somewhere ... I remember we discussed the name of such a property.

I think it's #3277 which adds a NewTab option to menus.

This one only covers adding target="_blank" which is a rather bad API choice IMO. Last update was that the author didn't know how to add tests for the feature as well as some discussion around naming.

The slight problem with this is that Hugo doesn't produce the href HTML constructs, it just supplies an URL and some title, and adding Target seems a little arbitrary and very tightly coupled to a concern that Hugo really does not care too much about (how does this work in ebook formats' menus?).

So, a better approach would maybe be to follow the .Params pattern we have elsewhere, so one could do:

$target := .Menu.Params.target

Or even maybe also on the menu item level.

Just a thought.

/cc @digitalcraftsman

Why not use a slice of identifiers either in a partial or layout where the menu is and for each of those identifiers in the slice set the target attribute?

So, a better approach would maybe be to follow the .Params pattern we have elsewhere, so one could do:

$target := .Menu.Params.target

Or even maybe also on the menu item level.

I would second this idea as it is known to Hugo users from other places. And it provides the maximum level of flexibility. If users have exotic requirements then they can add them as they like.

I like this idea too.. Opens up many more options, beyond just target.

This would stretch my Go skills (which is great), so I'm happy to try and implement. However if someone else wants to pick this up it and implement quicker than I will be able to, then I don't want to stand in the way :)

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

@crgeary - did you managed to implement that?
This could be great as in the moment the work arounds are pretty ugly for that.

@dudil sorry, couldn't figure it out (beyond my almost 0 knowledge of Go), then ended up forgetting all about it (oops).

@crgeary - thats Ok.
I'l tryl to look into it myself.

Was this page helpful?
0 / 5 - 0 ratings