Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Featured request: Auto-ID helper function #7734

Open
earthboundkid opened this issue Sep 25, 2020 · 16 comments
Open

Featured request: Auto-ID helper function #7734

earthboundkid opened this issue Sep 25, 2020 · 16 comments
Labels
Milestone

Comments

@earthboundkid
Copy link
Contributor

For some things, HTML requires unique-per-page ids. For example, a <label for="xxx"> and <input id="xxx"> pair. If you have a partial for a form, you need each form on the page to have unique IDs. Right now, I'm solving this with a .Scratch variable that just sets a counter per page. It works, but it's a little awkward because you have to pass .Page into a shortcode that needs an ID, and if you do it wrong you can accidentally reset the counter.

I propose a template helper function called autoid that returns a new ID every time you call it. It could take a prefix argument to make the IDs prettier for developers. Could work like this:

<!-- root file -->
<h1>Try our form!</h1>
{{ partial "form.html" . }}

<h2>No, please, try our form!</h2>
{{ partial "form.html" . }}

<!-- form.html -->
<label for="{{ $emailID := autoid "myform" }}">Email</label>
<input id="{{ $emailID }}" name="email">
<label for="{{ $nameID := autoid "myform" }}">Name</label>
<input id="{{ $nameID }}" name="name">

<!-- output -->
<h1>Try our form!</h1>
<label for="myform-0001">Email</label>
<input id="myform-0001" name="email">
<label for="myform-0002">Name</label>
<input id="myform-0002" name="name">

<h2>No, please, try our form!</h2>
<label for="myform-0003">Email</label>
<input id="myform-0003" name="email">
<label for="myform-0004">Name</label>
<input id="myform-0004" name="name">
@bep
Copy link
Member

bep commented Sep 25, 2020

This has been discussed before -- and the big problem of such function, if you make it something global, is that there is no way to preserve the ordering of things. And getting new IDs every time you run the site is not very friendly.

@earthboundkid
Copy link
Contributor Author

I think that can just be documented. "IDs subject to change on each build." Maybe to make people less likely to rely on the ID being the same spit out some hex gibberish, like myform-1D4EAC. Really, it just sucks that HTML requires "unique" IDs, but I don't think I can open an issue with W3C to get them to change… 😄

@moorereason
Copy link
Contributor

@carlmjohnson said:

It works, but it's a little awkward because you have to pass .Page into a shortcode that needs an ID, and if you do it wrong you can accidentally reset the counter.

Please explain the pain points. Why is your existing solution insufficient?

@earthboundkid
Copy link
Contributor Author

I thought I had a working page, but it turned out I was passing the wrong context to a partial, so the counter had reset itself in the loop.

@earthboundkid
Copy link
Contributor Author

spotlightpa/poor-richard@de9720c...af7e3fc#diff-36cf9d21926ba6b11981f69cc4da1571 It got covered up by force pushes, but basically this dot needed to be a $ to fix a bug in a loop. It was sort of subtle.

@github-actions
Copy link

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.

@github-actions
Copy link

github-actions bot commented Apr 2, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 2, 2022
@bep bep reopened this Jun 30, 2023
@bep bep added this to the v0.115.0 milestone Jun 30, 2023
@bep
Copy link
Member

bep commented Jun 30, 2023

I'll reopen this, because I find it better to write about these topics here rather than on the Discourse.

So, we already have math.Counter which I guess could be used to produce an ID ... but:

As you already experienced in https://discourse.gohugo.io/t/cachebuster-death-loop/45068/7 -- there are many reasons why you want this ID to be stable, for one, you would get massive change sets for your site on every build.

So, for most practical applications something like this should be OK:

{{ $pageIDString := .Page.RelPermalink | md5 }}
{{ $pageIDNumber := = .Page.RelPermalink | crypto.FNV32a  }}

@gohugoio gohugoio unlocked this conversation Jun 30, 2023
@earthboundkid
Copy link
Contributor Author

I don't see how making a hash of the page RelPermalink would address the problem at all. The issue to be solved is that I have form partials and I want to include that same partial an unknown number of times on one page. How does making up a semi-random ID once per URL address that?

@bep bep modified the milestones: v0.115.0, v0.116.0 Jun 30, 2023
@jmooring
Copy link
Member

@carlmjohnson Are the forms included on the page via a shortcode that calls a form partial?

@earthboundkid
Copy link
Contributor Author

Sometimes, yes. Also as plain partials.

@jmooring
Copy link
Member

jmooring commented Jun 30, 2023

When inserted via shortcode, you can use .Ordinal (e.g., printf "foo-%03d" .Ordinal). The .Ordinal is stable from one build to the next.

@earthboundkid
Copy link
Contributor Author

Another answer is to use Alpine's magic ID helper, but I'd rather not require JS for this functionality. https://alpinejs.dev/magics/id

@jmooring
Copy link
Member

If you don't mind unstable id's, bep's suggestion of math.Counter works, and you can use that in base templates, partials, and shortcodes.

@earthboundkid
Copy link
Contributor Author

I'm currently using a partial that adds one to a page.Store counter. It works but is apparently unstable.

@jmooring
Copy link
Member

My only other thought for partials (you can use .Ordinal for shortcodes) is...

layout/_default/single.html (or whatever)

{{ partial "foo.html" (dict "page" . "id" "6" }}
...
{{ partial "foo.html" (dict "page" . "id" "7" }}

And change it each time you call it.

@bep bep modified the milestones: v0.116.0, v0.117.0 Aug 1, 2023
@bep bep modified the milestones: v0.117.0, v0.118.0 Aug 30, 2023
@bep bep modified the milestones: v0.118.0, v0.119.0 Sep 15, 2023
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 4, 2023
@bep bep modified the milestones: v0.120.0, v0.121.0 Oct 31, 2023
@bep bep modified the milestones: v0.121.0, v0.122.0 Dec 6, 2023
@bep bep modified the milestones: v0.122.0, v0.123.0, v0.124.0 Jan 27, 2024
@bep bep modified the milestones: v0.124.0, v0.125.0 Mar 4, 2024
@bep bep modified the milestones: v0.125.0, v0.126.0 Apr 23, 2024
@bep bep modified the milestones: v0.126.0, v0.127.0 May 15, 2024
@bep bep modified the milestones: v0.127.0, v0.128.0 Jun 8, 2024
@bep bep modified the milestones: v0.128.0, v0.129.0 Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants