如何检查.Inner是否为空

问题描述

我正在尝试编写一个{{< gallery >}}代码,该短代码生成一个画廊,其中包含您定义的目录中的所有图像,通过.Inner定义的内容,或仅包含与之相关的所有图像资源。这一页。这是我要支持的表格:

  1. {{< gallery dir="/gallery/alaska/" />}}-指定的目录
  2. {{< gallery >}} {{< figure src="image1.jpg" >}} {{< /gallery >}}-指定的内部内容
  3. {{< gallery >}}-使用所有图像资源

我可以处理前两个,但是我不清楚如何确定.Inner变量中是否没有任何内容,因此可以处理上面的表格3。我希望做类似以下的事情:

{{- with (.Get "dir") -}}
  // do stuff with the specified directory (works fine)
{{- else -}}
  {{- if .Inner }}
    {{ .Inner }} // Always executes
  {{- else -}}
    // do stuff related to resources in this page
  {{- end }}
{{- end }}

如何检测没有参数也没有内部内容的裸短代码

解决方法

完成这项工作的关键是使用<tag/>形式的空标记,以便有内部内容,但它是空的。

这意味着如果您使用{{< gallery />}}作为简码,以下代码将起作用:

{{ with (.Get "dir") }}
  // do stuff with the directory
{{ else }}
  {{ with .Inner }}
    {{ . }}
  {{ else }}
    // do stuff related to this page
  {{ end }}
{{ end }}