Glob pattern to find files at the root and down the tree

I was never able to find the perfect glob pattern to find files at /image.jpg and /down-dir/image.jpg below the root dir.

I use /**/image.jpg and it will find any files at any level of the tree, but not the files at the root.

So I end up doing two calls. resources.Match /image.jpg and another resources.Match /**/image.jpg

**image.jpg or /**image.jpg does not work…

Any body knows if a single pattern could find both files?

1 Like
content/
β”œβ”€β”€ posts/
β”‚   └── post-1/
β”‚       β”œβ”€β”€ page_resources/
β”‚       β”‚   └── images/
β”‚       β”‚       └── kittens/
β”‚       β”‚           β”œβ”€β”€ a.jpg
β”‚       β”‚           β”œβ”€β”€ b.jpg
β”‚       β”‚           β”œβ”€β”€ c.jpg
β”‚       β”‚           └── d.jpg
β”‚       └── index.md
└── _index.md

layouts/_default/single.html

{{ with .Resources.Match "**/*.jpg" }}
  {{ range . }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}

Slightly different example:

content/
β”œβ”€β”€ posts/
β”‚   └── post-1/
β”‚       β”œβ”€β”€ page_resources/
β”‚       β”‚   β”œβ”€β”€ images/
β”‚       β”‚   β”‚   β”œβ”€β”€ kittens/
β”‚       β”‚   β”‚   β”‚   └── d.jpg
β”‚       β”‚   β”‚   └── c.jpg
β”‚       β”‚   └── b.jpg
β”‚       β”œβ”€β”€ a.jpg
β”‚       └── index.md
└── _index.md

layouts/_default/single.html

{{ with .Resources.Match "**.jpg" }}
  {{ range . }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}
1 Like

I see then I guess it works in this context. Now I’m trying with files whose basename I know, but not the extension. So the glob pattern should be **data.* but it does not work (this is with resources.Match)

assets/
β”œβ”€β”€ kittens/
β”‚  └── data.json
β”‚  └── data.yaml
└── data.json

With the above, the **data.* pattern only returns the kittens files.

I think this is a typo? site.Resources.Match

Typo yes :slight_smile:

assets/
β”œβ”€β”€ global_resources/
β”‚   β”œβ”€β”€ data/
β”‚   β”‚   β”œβ”€β”€ kittens/
β”‚   β”‚   β”‚   └── data.json
β”‚   β”‚   └── data.json
β”‚   β”œβ”€β”€ images/
β”‚   β”‚   β”œβ”€β”€ kittens/
β”‚   β”‚   β”‚   └── d.jpg
β”‚   β”‚   └── c.jpg
β”‚   β”œβ”€β”€ b.jpg
β”‚   └── data.json
β”œβ”€β”€ a.jpg
└── data.json

layouts/_default/single.html

{{ with resources.Match "**data.*" }}
  {{ range . }}
    {{ .RelPermalink }}<br>
  {{ end }}
{{ end }}

rendered

/data.json
/global_resources/data/data.json
/global_resources/data/kittens/data.json
/global_resources/data.json
1 Like

Thanks for testing this :pray:

I’ll keep looking into my project and try and figure out what I missed!