Last active
August 8, 2022 22:36
-
-
Save neodigm/3f20026dd8462447e987653b77ff4c9d to your computer and use it in GitHub Desktop.
Hugo Go Template Short code list logic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
hugo new accelerators/ga4.html | |
hugo new accelerators/webdev.html | |
--> | |
{{ $dir := (.Get "dir") }} | |
{{ if eq $dir "" }} {{ $dir = "assets" }} {{ end }} | |
{{/* | |
get all files that matches the directory name | |
which is passed as argument dir="" to the shortcode: | |
*/}} | |
{{ $assets := .Page.Resources.Match (printf "%s/*" $dir) }} | |
{{ $assetdir := (printf "%s/%s" (.Page.Path | path.Dir) $dir) }} | |
{{ $page_resources := .Page.Resources.ByType "image" }} | |
<table class="table table-responsive table-condensed"> | |
<th>Name:</th> | |
<th><span style="padding-left:20px"></th> | |
<th>Size <small>/ byte</small>:</th> | |
{{- range $assets -}} | |
<!-- took me a while to figure this out: --> | |
{{ $fname := . | path.Base }} | |
{{ $fstat := os.Stat (printf "%s/%s" $assetdir $fname ) }} | |
<tr> | |
<td> | |
<!-- if the actual file is an image, | |
we'd like to show a small thumbnail image... --> | |
<div> | |
{{ if ($page_resources.GetMatch (printf "*%s*" . )) }} | |
<img src="{{ .Resize "60x60" }}" width="60px"> | |
{{ else if eq (. | path.Ext ) ".pdf" }} | |
<i class="icon-file-pdf1" style="font-size:22px;color:Orange" ></i> | |
{{ else }} | |
<i class="icon-line-file" style="font-size:22px;color:DarkGrey" ></i> | |
{{ end }} | |
<a href="{{ .RelPermalink }}"> {{ . | path.Base }} </a> | |
</div> | |
</td> | |
<th></th> | |
<td> {{ $fstat.Size }} </td> | |
</tr> | |
{{- end -}} | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment