【问题标题】:Concatenate variable in HugoHugo 中的连接变量
【发布时间】:2018-03-28 01:00:08
【问题描述】:

这个 sn-p 在 Hugo 上效果很好:

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

^显然,这是行不通的。我想知道有什么作用。

【问题讨论】:

    标签: hugo


    【解决方案1】:

    在 Hugo 讨论论坛上,有人 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 函数已被使用。

    hugo 文档是这样说的:

    如果条件和类似条件在外部不可见,则在内部定义变量。

    (see this issue)

    这是一种临时存储值的解决方法。 here's more on scratch

    限制

    截至目前,我觉得这段代码并不完整。它有效,但是它根据提交显示作者。所以多次提交将多次生成同一个作者。我将这个限制提请您注意,以开发一个创造性的解决方案。当我得到满意的答案时,我会更新这个答案。同时,请随时提出建议。

    here是我对雨果话语的原始回答。

    【讨论】:

      猜你喜欢
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多