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

Generate a random list of posts [Feature request] #1942

Closed
Jos512 opened this Issue Mar 9, 2016 · 3 comments

Comments

Projects
None yet
3 participants
@Jos512

Jos512 commented Mar 9, 2016

I'd like to generate a random* list of post in Hugo; the idea is to place links to the first 5 of these below the posts as a way of "read more".

For this I was thinking about something like:

{{ range first 5 (where .Data.Pages.Random "Section" "post") }}
   {{ .Title }}
{{ end }}

The idea is that Hugo, instead of generating a list ordered by date (.Data.Pages.ByDate), a list generates with all posts in .Data.Pages ordered in a random manner. Then we can use the existing range, first, and where functions to "pull" the number of random posts from the list as we'd like.

One requirement for this is that Hugo generates a random list of posts with a new seed each time .Data.Pages.Random is come across. Otherwise the list of posts will be random between builds, but not during builds.

*: I know that such a list would be, theoretically speaking, pseudo-random. But I'm more than fine with a "seemingly random" list, especially since that would be easier to implement and is much friendlier on the build times.

(A related forum topic is here: https://discuss.gohugo.io/t/psuedo-random-list-of-category-posts/1923 ).

@christophermancini

This comment has been minimized.

Show comment
Hide comment
@christophermancini

christophermancini Mar 9, 2016

Contributor

It might be a bit easier to understand for a user if we added a random template function that randomly selects elements from an array, map, or slice. To me, your recommendation makes me think they are randomly sorted, which I am not sure is a common use case or efficient processing wise.

For instance, with a random function, we could have the following syntax and it will return a slice with a max of 5 items randomly selected from the input list.

range random 5 (where .Data.Pages "Section" "post")

The only con I see to this, is that it could appear as if your data on your page is changing regularly, so would need to keep that in mind w.r.t. SEO and how you use HTML5 elements.

EDIT: Looks like on the master branch is a template function called shuffle which you could likely use in conjunction with first to accomplish your task. I don't know for sure yet though.

Contributor

christophermancini commented Mar 9, 2016

It might be a bit easier to understand for a user if we added a random template function that randomly selects elements from an array, map, or slice. To me, your recommendation makes me think they are randomly sorted, which I am not sure is a common use case or efficient processing wise.

For instance, with a random function, we could have the following syntax and it will return a slice with a max of 5 items randomly selected from the input list.

range random 5 (where .Data.Pages "Section" "post")

The only con I see to this, is that it could appear as if your data on your page is changing regularly, so would need to keep that in mind w.r.t. SEO and how you use HTML5 elements.

EDIT: Looks like on the master branch is a template function called shuffle which you could likely use in conjunction with first to accomplish your task. I don't know for sure yet though.

@digitalcraftsman

This comment has been minimized.

Show comment
Hide comment
@digitalcraftsman

digitalcraftsman Mar 9, 2016

Member

Hello @christophermancini, @Jos512

Take a look at #1679. @christophermancini assumption is right. As the purpose of shuffle states:

The function can be used to generate random content to the sites. For example to display random quote or display list of random blog posts.

This should satisfy you needs.

Member

digitalcraftsman commented Mar 9, 2016

Hello @christophermancini, @Jos512

Take a look at #1679. @christophermancini assumption is right. As the purpose of shuffle states:

The function can be used to generate random content to the sites. For example to display random quote or display list of random blog posts.

This should satisfy you needs.

@Jos512

This comment has been minimized.

Show comment
Hide comment
@Jos512

Jos512 Mar 9, 2016

Thanks for the responses Christopher and DigitalCraftsman. shuffle seems to do what I was hoping to do, so that's very great.

Look forward to using it in 0.16! 👍

Jos512 commented Mar 9, 2016

Thanks for the responses Christopher and DigitalCraftsman. shuffle seems to do what I was hoping to do, so that's very great.

Look forward to using it in 0.16! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment