HUGO

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
Star

What's on this Page

    • Traverse Local Files
    • Use readDir
      • readDir Example: List Directory Files
    • Use readFile
      • readFile Example: Add a Project File to Content
TEMPLATES

Local File Templates

Hugo’s readerDir and readFile functions make it easy to traverse your project’s directory structure and write file contents to your templates.

Traverse Local Files

With Hugo’s readDir and readFile template functions, you can traverse your website’s files on your server.

Use readDir

The readDir function returns an array of os.FileInfo. 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).

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:

  1. The file system root
  2. The current working directory

readDir Example: List Directory Files

This shortcode creates a link to each of the files in a directory—display as the file’s basename—along with the file’s size in bytes.

layouts/shortcodes/directoryindex.html

{{- $pathURL := .Get "pathURL" -}}
{{- $path := .Get "path" -}}
{{- $files := readDir $path -}}
<table>
    <th>Size in bytes</th>
    <th>Name</th>
{{- range $files }}
    <tr>
        <td>{{ .Size }}</td>
        <td><a href="{{ $pathURL }}{{ .Name | relURL }}" target="_blank"> {{ .Name }}</a></td>
    </tr>
{{- end }}
</table>

You can then call the shortcode as follows inside of your content’s markup:

{{< directoryindex path="/static/css" pathURL="/css" >}}

The above shortcode is part of the code for the Hugo docs. Here it lists this site’s CSS files:

Size in bytesName
7612android-chrome-144x144.png
10264android-chrome-192x192.png
15088android-chrome-256x256.png
1592android-chrome-36x36.png
2038android-chrome-48x48.png
3467android-chrome-72x72.png
4747android-chrome-96x96.png
6238apple-touch-icon.png
335browserconfig.xml
1000favicon-16x16.png
1648favicon-32x32.png
15086favicon.ico
4096fonts
4096images
1141manifest.json
6225mstile-144x144.png
6020mstile-150x150.png
12885mstile-310x310.png
1112safari-pinned-tab.svg

The initial slash / in pathURL is important in the directoryindex shortcode. Otherwise, pathURL becomes relative to the current web page.

Use readFile

The readfile function 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.

To use the readFile function in your templates, make sure the path is relative to your Hugo project’s root directory:

{{ readFile "/content/templates/local-file-templates" }}

readFile Example: Add a Project File to Content

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 that calls readFile, passes the first argument through the function, and then allows an optional second argument to send the file through the Blackfriday markdown processor. The pattern for adding this shortcode to your content will be as follows:

{{< readfile file="/path/to/local/file.txt" markdown="true" >}}

If you are going to create custom shortcodes with readFile for a theme, note that usage of the shortcode will refer to the project root and not your themes directory.

Here is the templating for our new readfile shortcode:

layouts/shortcodes/readfile.html

{{$file := .Get "file"}}
{{- if eq (.Get "markdown") "true" -}}
{{- $file  | readFile | markdownify -}}
{{- else -}}
{{ $file  | readFile | safeHTML }}
{{- end -}}

This readfile shortcode is also part of the Hugo docs. So is testing.txt, which we will call in this example by passing it into our new readfile shortcode as follows:

{{< readfile file="/content/en/readfiles/testing.txt" >}}

The output “string” for this shortcode declaration will be the following:

##### Hello World!

Testing one, **two**, *three*. Don't delete this sample file used in the [templates](/templates/) section of the Hugo docs.

However, if we want Hugo to pass this string through Blackfriday, we should add the markdown="true" optional parameter:

{{< readfile file="/content/en/readfiles/testing.txt" markdown="true" >}}

And here is the result as called directly in the Hugo docs and rendered for display:

Hello World!

Testing one, two, three. Don’t delete this sample file used in the templates section of the Hugo docs.

See Also

  • Static Files
  • readDir
  • Directory Structure
  • readFile
  • Create a Theme
  • About Hugo
    • Overview
    • Hugo and GDPR
    • Hugo 0.32 HOWTO
    • What is Hugo
    • Hugo Features
    • The Benefits of Static
    • License
  • Getting Started
    • Get Started Overview
    • Quick Start
    • Install Hugo
    • Basic Usage
    • Directory Structure
    • Configuration
  • Themes
    • Themes Overview
    • Install and Use Themes
    • Theme Components
    • Create a Theme
  • Content Management
    • Content Management Overview
    • Organization
    • Page Bundles
    • Supported Content Formats
    • Front Matter
    • Page Resources
    • Image Processing
    • Shortcodes
    • Related Content
    • Sections
    • Types
    • Archetypes
    • Taxonomies
    • Summaries
    • Links and Cross References
    • URL Management
    • Menus
    • Static Files
    • Table of Contents
    • Comments
    • Multilingual and i18n
    • Syntax Highlighting
  • Templates
    • Templates Overview
    • Introduction
    • Template Lookup Order
    • Custom Output Formats
    • Base Templates and Blocks
    • List Page Templates
    • Homepage Template
    • Section Templates
    • Taxonomy Templates
    • Single Page Templates
    • Content View Templates
    • Data Templates
    • Partial Templates
    • Shortcode Templates
    • Local File Templates
    • 404 Page
    • Menu Templates
    • Pagination
    • RSS Templates
    • Sitemap Template
    • Robots.txt
    • Internal Templates
    • Alternative Templating
    • Template Debugging
  • Functions
    • Functions Quick Reference
    • Built-in
    • Cast
    • Collections
    • Compare
    • Crypto
    • Data
    • Encoding
    • Fmt
    • Hugo
    • Images
    • Inflect
    • Lang
    • Math
    • OS
    • Partials
    • Path
    • Reflect
    • Resources
    • Safe
    • Site
    • Strings
    • Templates
    • Time
    • Transform
    • URLs
  • Objects
    • Objects Overview
    • Site Objects
    • Page Objects
    • Shortcode Objects
    • Time Objects
    • Taxonomy Objects
    • File Objects
    • Menu Entry Objects
    • Hugo Objects
    • Git Objects
    • Sitemap Objects
  • Hugo Pipes
    • Hugo Pipes Overview
    • Hugo Pipes Introduction
    • SASS / SCSS
    • PostCSS
    • Asset minification
    • Asset bundling
    • Fingerprinting and SRI
    • Resource from Template
    • Resource from String
  • CLI
  • Troubleshooting
    • Troubleshoot
    • FAQ
    • Build Performance
  • Tools
    • Developer Tools Overview
    • Migrations
    • Starter Kits
    • Frontends
    • Editor Plug-ins
    • Search
    • Other Projects
  • Hosting & Deployment
    • Hosting & Deployment Overview
    • Host-Agnostic Deploys with Nanobox
    • Host on Netlify
    • Host on Firebase
    • Host on GitHub
    • Host on GitLab
    • Hosting on KeyCDN
    • Host on Bitbucket
    • Deployment with Wercker
    • Deployment with Rsync
  • Contribute
    • Contribute to Hugo
    • Development
    • Documentation
    • Themes
  • Maintenance
“Local File Templates” was last updated: November 25, 2018: Update asset dependencies and adopt Hugo Pipes (fd77e8df)
Improve this page
By the Hugo Authors
Hugo Logo
  • File an Issue
  • Get Help
  • Discuss Source Code
  • @GoHugoIO
  • @spf13
  • @bepsays

 
  Hugo Sponsors
Logo for Forestry.io
Logo for Linode
Logo for eSolia
 

The Hugo logos are copyright © Steve Francia 2013–2019.

The Hugo Gopher is based on an original work by Renée French.

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
  • About Hugo
  • Getting Started
  • Themes
  • Content Management
  • Templates
  • Functions
  • Objects
  • Hugo Pipes
  • CLI
  • Troubleshooting
  • Tools
  • Hosting & Deployment
  • Contribute
  • Maintenance