hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

files.md (2955B)

    1 ---
    2 title: Local File Templates
    3 linktitle: Local File Templates
    4 description: Hugo's `readDir` and `readFile` functions make it easy to traverse your project's directory structure and write file contents to your templates.
    5 date: 2017-02-01
    6 publishdate: 2017-02-01
    7 lastmod: 2017-02-01
    8 categories: [templates]
    9 keywords: [files,directories]
   10 menu:
   11   docs:
   12     parent: "templates"
   13     weight: 110
   14 weight: 110
   15 sections_weight: 110
   16 draft: false
   17 aliases: [/extras/localfiles/,/templates/local-files/]
   18 toc: true
   19 ---
   20 
   21 ## Traverse Local Files
   22 
   23 With Hugo's [`readDir`][readDir] and [`readFile`][readFile] template functions, you can traverse your website's files on your server.
   24 
   25 ## Use `readDir`
   26 
   27 The [`readDir` function][readDir] returns an array of [`os.FileInfo`][osfileinfo]. It takes the file's `path` as a single string argument. This path can be to any directory of your website (i.e., as found on your server's file system).
   28 
   29 Whether the path is absolute or relative does not matter because---at least for `readDir`---the root of your website (typically `./public/`) in effect becomes both:
   30 
   31 1. The file system root
   32 2. The current working directory
   33 
   34 ## Use `readFile`
   35 
   36 The [`readfile` function][readFile] reads a file from disk and converts it into a string to be manipulated by other Hugo functions or added as-is. `readFile` takes the file, including path, as an argument passed to the function.
   37 
   38 To use the `readFile` function in your templates, make sure the path is relative to your *Hugo project's root directory*:
   39 
   40 ```
   41 {{ readFile "/content/templates/local-file-templates" }}
   42 ```
   43 
   44 ### `readFile` Example: Add a Project File to Content
   45 
   46 As `readFile` is a function, it is only available to you in your templates and not your content. However, we can create a simple [shortcode template][sct] that calls `readFile`, passes the first argument through the function, and then allows an optional second argument to send the file through the markdown processor. The pattern for adding this shortcode to your content will be as follows:
   47 
   48 ```
   49 {{</* readfile file="/path/to/local/file.txt" markdown="true" */>}}
   50 ```
   51 
   52 {{% warning %}}
   53 If you are going to create [custom shortcodes](/templates/shortcode-templates/) with `readFile` for a theme, note that usage of the shortcode will refer to the project root and *not* your `themes` directory.
   54 {{% /warning %}}
   55 
   56 
   57 
   58 [called directly in the Hugo docs]: https://github.com/gohugoio/hugoDocs/blob/master/content/en/templates/files.md
   59 [dirindex]: https://github.com/gohugoio/hugo/blob/master/docs/layouts/shortcodes/directoryindex.html
   60 [osfileinfo]: https://golang.org/pkg/os/#FileInfo
   61 [readDir]: /functions/readdir/
   62 [readFile]: /functions/readfile/
   63 [sc]: /content-management/shortcodes/
   64 [sct]: /templates/shortcode-templates/
   65 [readfilesource]: https://github.com/gohugoio/hugoDocs/blob/master/layouts/shortcodes/readfile.html
   66 [testfile]: https://github.com/gohugoio/hugoDocs/blob/master/content/en/readfiles/testing.txt