With,
[Params]
contentCommitsURL = https://api.github.com/repos/csitauthority/CSITauthority.github.io/commits?path=HUGO/content/
the following snippet works wonderfully, on hugo, to generate a Link to API call from a post layout
{{ if and (or .IsPage .IsSection) .Site.Params.contentCommitsURL }}
{{ $File := .File }}
{{ $Site := .Site }}
{{with $File.Path }}
<a href="{{ $Site.Params.contentCommitsURL }}{{ replace $File.Dir "\\" "/" }}{{ $File.LogicalName }}" target="blank">Link to API call</a>
{{ end }}
{{ end }}
Problem Description
The url is generated but I’m pulling hairs trying to figure out how to save the result in a variable
.
I want to be able to do something like this:
{{ $url := $Site.Params.contentCommitsURL + (replace $File.Dir "\\" "/") + $File.LogicalName }}
so that I can later access {{ $url }}
and use as desired. Perhaps like <a href="{{$url}}">Link to API call</a>
. Or, for other purposes like {{ range getJSON $url }}
I think print is what you’re looking for
as a generalish example:
{{$base := $.Param "contentCommitsURL"}}
{{$foo := "bar"}}
{{$url := print $base "/" $foo}}
3 Likes
I was able to solve that issue nicely as follows:
{{ if and (or .IsPage .IsSection) .Site.Params.contentCommitsURL }}
{{ $File := .File }}
{{ $Site := .Site }}
{{with $File.Path }}
{{ $fileDir := replace $File.Dir "\\" "/"}}
{{ $url := $File.LogicalName | printf "%s%s" $fileDir | printf "%s%s" $Site.Params.contentCommitsURL }}
{{ $.Scratch.Set "url" $url }}
{{ end }}
{{ end }}
Then, where I want the page contributors to appear
{{ $url := $.Scratch.Get "url"}}
{{ range getJSON $url }}
<div style="display:inline-block; width:40px;"><a href="{{.author.html_url}}" target="_blank">
<img src="{{.author.avatar_url}}" alt="{{.author.login}}" text="{{.author.login}}" class="inline" width="40" height="40" style="height: 40px;height: 40px; vertical-align:middle; margin: 0 auto;"></a>
</div>
{{ end }}
But alas, the human mind is never satisfied…
To make this perfect, I would like to know how I can filter distinct {{ .author.login }}
. I am getting same authors being returned multiple times *
( * = equal to the number of times that the author has committed to that particular file.)
An example would be: this returns siddhantrimal
twice!
I need it to return once. If multiple authors, I need to list each distinct author.