2017-10-16 71 views
0

這個片段奇妙的作品在雨果:串聯可變雨果

{{ 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 }} 

有了,

[Params] 
contentCommitsURL = https://api.github.com/repos/csitauthority/CSITauthority.github.io/commits?path=HUGO/content/ 

它能夠漂亮地生成以下鏈接

Link to API call

layout html文件。


問題描述

生成的URL。現在我拉頭髮試圖找出如何連接命令{ $Site.Params.contentCommitsURL }}{{ replace $File.Dir "\\" "/" }}{{ $File.LogicalName }}在頁面中的變量,例如{{ $url }}

例如:

{{ $url := {{ $Site.Params.contentCommitsURL }}{{ replace $File.Dir "\\" "/" }}{{ $File.LogicalName }} }} 

不起作用

但下列情況:

{{ $url := "https://api.github.com/repos/csitauthority/CSITauthority.github.io/commits?path=HUGO/content/post/vlan-101.md"}} 

我希望能夠做到這樣的事情:

{{ $url := $Site.Params.contentCommitsURL + (replace $File.Dir "\\" "/") + $File.LogicalName }} 

^顯然,這是行不通的。我想知道是什麼。

回答

0

在雨果話語論壇上,有人hinted a solution和我能夠想出以下內容。

{{ 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 }} 

,我想它出現,我用的是Scratch功能是這樣的:

{{ $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 }} 

的代碼是不言自明的,所以我不會有詳細的描述麻煩。相反,我想把你的重點放在實施上。您會注意到已使用Scratch功能。

雨果文檔說這個:定義裏面,如果條件句和類似不在外面看到

變量。

(see this issue)

這涉及臨時存儲的值的解決方法。 here's more on scratch

限制

由於這一刻,我覺得這個代碼是不完整的。它的工作原理,但它顯示了作者基於提交。所以多次提交會多次生成相同的作者。我將這一限制引入您的注意事項,以開發創造性解決方案。當我得到滿意的答案時,我會更新這個答案。同時,隨時提出建議。

here是我對hugo話語的原始回答。