config.toml has the following authors defined: (hash, guest)
[params.authors]
[params.authors.hash]
name = "Hash Borgir"
bio = "Hash is a Software Developer and generally a tech geek and computer nerd. He also consumes massive quantities of Cannabis and takes extremely deep journeys inwardly, meditates, explores the Universe within."
location = "Colorado, USA"
email = ""
twitter= "HashBorgir"
image = "/images/authors/Hash_Borgir/user.jpg"
instagram = "hashborgir"
[params.authors.guest]
name = "Guest Writer"
bio = "Guest author profile. No bio. "
location = "Colorado, USA"
email = ""
twitter= ""
image = "/images/authors/Guest/Manbrown2.png"
instagram = ""
Article front matter is like this:
+++
author = “hash” which corresponds to .Site.Params.authors.hash
+++
This is my partial for listing all the authors as defined in the site on the sidebar.
{{ $baseurl := .Site.BaseURL }}
<div class="panel sidebar-menu">
<div class="panel-heading">
<h2 class="panel-title">Authors</h23>
</div>
<div class="panel-body">
<ul class="authors-list nav nav-pills nav-stacked">
{{ range $name, $items := .Site.Params.authors }}
{{ range $key, $val := $items }}
{{ if eq $key "name" }}
<li><a href="{{ $baseurl }}authors/{{ $val | urlize | lower }}"><i class="fa fa-user"></i> {{ $val }}</a>
</li>
{{ end }}
{{ end }}
{{ end }}
</ul>
</div>
</div>
<hr />
What I want to do, is display in (parentheses) the number of articles by this author. I am having much trouble with the syntax of this language/template langauge etc.
I try this: and many variations and can’t seem to get this right to get the # of posts by a certain author.
{{ range $name, $items := .Site.Params.authors }}
{{ range $key, $val := $items }}
{{ if eq $key "name" }}
{{range $k, $v := (where .Site.Pages "Author" $key) }}
<li><a href="{{ $baseurl }}authors/{{ $val | urlize | lower }}"><i class="fa fa-user"></i> {{ $val }}{{len $k}}</a>
</li>
{{ end }}
{{ end }}
{{ end }}
{{ end }}
Please advise.
I’m not sure how far you are into the dev of your site and you have definitely thrown out a lot of content here in the forums over the last 24 hours
FYI and before I make suggestions, see the following so that you know that author metadata is on the milestones list for the next Hugo v:
https://github.com/spf13/hugo/pull/1850
With the current version of Hugo, you might be able to handle this easily with taxonomies; aka, authors
in your site’s configuration. Then you would be able to list ALL articles by a certain author without having to do any extra work.
With the advent of “everything is a page,” you should be able to add author information (i.e., metadata/front matter and content) via individual _index.md
files.
Here are a couple places to check:
Content Organization (will give you a run-down of _index.md
): https://hugodocs.info/content-management/organization/
And also List page template (will show you where to add and how to call these files): https://hugodocs.info/templates/lists/#what-is-a-list-page-template
This approach will make it much easier for you to grab len
as well for a list of .Data.Pages
on a list page. This also helps you because it means you can add author-list pages auto-magically whenever you add a new author to a piece of content; e.g.—
authors: [Hash Borgir, Ryan Watters, Jon Doe, Benjamin Franklin]
Even if you only have two files for authors/hash-borgir/_index.md
and authors/ryan-watters/_index.md
, the final output url for yoursite.com/jon-doe
and yoursite.comd/benjamin-franklin/
will still be generated with the list you define in the taxonomy terms template but will pull default values rather than those definied in _index.md
files.
Does that help?
All the articles on my site have front matter
author = “hash” which maps to .Site.Params.authors.hash (which is an array, which contains author profile info)
All I’m trying to do is get the array which has all the pages from a certain author.
{{ range $name, $items := .Site.Params.authors }}
{{ range $key, $val := $items }}
{{ if eq $key "name" }}
<li>
<a href="{{ $baseurl }}authors/{{ $val | urlize | lower }}"><i class="fa fa-user"></i> {{ $val }}
</a>
{{ range $a, $b := index (where .Site.Pages "Params.author" $key) }}
{{ len $a }}
{{ end }}
</li>
{{ end }}
{{ end }}
{{ end }}
see this:
{{ range $a, $b := index (where .Site.Pages "Params.author" $key) }}
{{ len $a }}
{{ end }}
$key is “hash” and I am trying to count the number of items in the array which contains all the pages written by author hash.
What you have told me is a great solution but requires me to redevelop my whole site and that’s not possible at the moment.
All I am trying to do right now is figure out this template syntax and be able to get and iterate over arrays.
Forget I mentioned author.
If some of the articles on my website have front matter
donkey=“egore”
I want to get the number of articles who have donkey=“egore” defined. I want to be able to define which donkey to search for, and get the # of articles which have that particular donkey. Hehe, more silly goofy stuff.
{{ range $a, $b := index (where .Site.Pages “Params.donkey” $donkeyname }}
{{ len $a }}
{{ end }}
I have no idea what to do.
- Can you point me to a repo I can pull and test locally?
The suggestion I provided will take care of all your Eeyore-related needs.
Thanks for noticin’ me.
That requires me to redo just about the whole of everything. sigh
Haha, I’m not saying you have to redo anything; I just want you to point me to a repo so i can test it and help you.
I’m sure can get this all done at the templating, so don’t worry.
I sent a private message with repository link.
If you just want the number, this works, but I think there are DRYer ways to do it (just too busy right now)…
<!--This assumes the authors are being added in front matter in an array; if not, just use $author := .Params.author-->
{{$author := index (.Params.authors) 0 }}
{{$.Scratch.Set "arts" 0}}
{{ range .Site.Pages }}
{{ if in .Params.authors $author}}
{{$.Scratch.Add "arts" 1}}
{{ end }}
{{ end }}
{{$.Scratch.Get "arts"}}
If you want to get a list of the articles themselves…
<!--This assumes the authors are being added in front matter in an array; if not, just use $author := .Params.author-->
{{$author := index (.Params.authors) 0 }}
<ul class="author-articles">
{{ range where .Site.Pages ".Params.author" $author }}
<li>Whatever it is you want to add here....</li>
{{ end }}
</ul>
Keep in mind the above doesn’t sort, but you can do that via multiple methods.
Following is not tested
I can tell you that this is at least an issue…
{{range $k, $v := (where .Site.Pages "Author" $key) }}
Author
isn’t like Section
, so you have to treat it as a param…
{{range $k, $v := (where .Site.Pages ".Params.author" $key) }}
As far as getting the length…you’re inside a loop, so I would suggest looking at the scratch method I used above.
Just don’t say I didn’t warn you about how this would be easier using taxonomies…
1 Like
Sir, I AM using taxonomies.
BTW, this is all being done in a partial template, not a list template, not an _index template, not anything but a partial for my sidebar.
I have no idea how else to describe my issue.
May we please just forget about authors?
I’m just trying to count articles that have a certain front matter defined, that’s all.
If you’re trying to count the number of matches, you can use len
directly:
{{ len (where .Site.Pages ".Params.author" "hash") }}
Or you could assign it with something like $len := len...
.
Oops. Just saw “partial” in your message. You may want $.Site.Pages to escape the dot.
{{ range $name, $items := .Site.Params.authors }}
{{ range $key, $val := $items }}
{{ if eq $key "name" }}
<li>
<a href="{{ $baseurl }}authors/{{ $val | urlize | lower }}"><i class="fa fa-user"></i> {{ $val }}
</a>
</li>
{{ len (where .Site.Pages ".Params.author" "hash") }}
{{ end }}
{{ end }}
{{ end }}
ERROR 2017/03/19 16:54:25 theme/partials/widgets/author.html template: theme/partials/widgets/author.html:20:43: executing “theme/partials/widgets/author.html” at <.Site.Pages>: can’t evaluate field Site in type interface {}
Try $.Site.Pages to escape the dot.
1 Like
Are you telling me THAT is why it didn’t work for two day and now I have less hair on my head?
sigh $.Site.Pages works.
My old code works now…
Can you please explain b/c I do not understand this language much.
It’s scope. When you’re in the range
, the rules for Go templates say that dot (the .
) binds to the values in the range. You have to use the $.
to escape that and go to the global values. That’s a Go templating issue - it’s not unique to Hugo.
2 Likes
Oh wow, man. Thank you so much! That instantly fixes my issues.
{{ range $name, $items := .Site.Params.authors }}
{{ range $key, $val := $items }}
{{ if eq $key "name" }}
<li>
<a href="{{ $baseurl }}authors/{{ $val | urlize | lower }}"><i class="fa fa-user"></i> {{ $val }}
({{ len (where $.Site.Pages ".Params.author" $name) }})
</a>
</li>
{{ end }}
{{ end }}
{{ end }}
EDIT: And testing now w/ the guest author who only has one article:
1 Like
So now, I want to figure out what you’re talking about. Might take me a day or two, but I can start another branch on Gitlab and try out the changes there, as you suggest.
Oh, the _index thing is new since we last talked and I did some Hugo templating.
(Since the I’ve been writing mostly and didn’t have to do Hugo template work, but now for a new site and there are new feautres I dunno about. This is pretty cool, thanks!)
1 Like
Oops. For authors? Sorry, if I had gotten into your repo, I probably would have seen that in your config.
Thank you @michael_henderson!