diff --git a/content/extras/localfiles.md b/content/extras/localfiles.md new file mode 100644 index 000000000..843082df9 --- /dev/null +++ b/content/extras/localfiles.md @@ -0,0 +1,51 @@ +--- +aliases: +- /doc/localfiles/ +date: 2015-06-12 +menu: + main: + parent: extras +next: /community/mailing-list +notoc: true +prev: /extras/urls +title: Traversing Local Files +weight: 110 +--- + +## Traversing Local Files + +Hugo includes a way to traverse local files. +This is done using the 'ReadDir' function. + +## Using ReadDir + +ReadDir takes a single string input that is relative to the root directory of the site. It returns an array of [os.FileInfo](https://golang.org/pkg/os/#FileInfo) + +Let's create a shortcode to build a file index with links using ReadDir. + +'fileindex.html' + + + + + {{$dir := .Get "dir"}} + {{ $url := .Get "baseurl" }} + + {{ $files := ReadDir $dir }} + {{ range $files }} + + + + + {{ end }} +
Size in bytesName
{{.Size}} + {{.Name}} +
+ +Now lets use it to list the css files used on this site + + {{}} + +Is rendered as: + +{{< fileindex dir="static/css/" baseurl="/css/">}} diff --git a/layouts/shortcodes/fileindex.html b/layouts/shortcodes/fileindex.html new file mode 100644 index 000000000..132ed67e5 --- /dev/null +++ b/layouts/shortcodes/fileindex.html @@ -0,0 +1,16 @@ + + + +{{$dir := .Get "dir"}} +{{ $url := .Get "baseurl" }} + +{{ $files := ReadDir $dir }} + {{ range $files }} + + + + + {{ end }} +
Size in bytesName
{{.Size}} + {{.Name}} +