File System Helpers

Handlebars provides a set of built-in helpers for working with the file system. These helpers are used to read and manipulate files, making it easier to work with file data in templates.

fs

{{fileSize}}

Formats a number of bytes into a human-readable file size string with appropriate units.

Params

  • value {Number|Object}: The number of bytes, or an object with a length property
  • precision {Number}: Optional decimal precision (default: 2)
  • returns {String}: Formatted file size string

Supported Units

B, kB, MB, GB, TB, PB, EB, ZB, YB

Example

{{fileSize 1024}}


{{fileSize 1536}}


{{fileSize 1048576}}


{{fileSize 1073741824}}



{{fileSize 1536 0}}


{{fileSize 1536 3}}



{{fileSize null}}

{{read}}

Read a file from the file system. This is useful in composing "include"-style helpers using sub-expressions.

Params

  • filepath {String}: The path to the file to read
  • returns {String}: The file contents as a UTF-8 string

Example


{{read "path/to/file.txt"}}


{{markdown (read "README.md")}}



{{readdir}}

Return an array of files from the given directory. Supports optional filtering by function, RegExp, glob pattern, or type.

Params

  • directory {String}: The directory path to read
  • filter {Function|RegExp|String}: Optional filter to apply to the file list
  • returns {Array}: Array of file paths

Filter Options

  • Function: Custom filter function that receives the files array and returns filtered array
  • RegExp: Regular expression to test against file paths
  • Glob string: Glob pattern to match files (e.g., "*.js", "**/*.md")
  • "isFile": Return only files (not directories)
  • "isDirectory": Return only directories (not files)

Example


{{#each (readdir "src")}}
  {{this}}
{{/each}}


{{#each (readdir "src" "*.js")}}
  {{this}}
{{/each}}


{{#each (readdir "src" "isFile")}}
  {{this}}
{{/each}}


{{#each (readdir "src" "isDirectory")}}
  {{this}}
{{/each}}


{{#each (readdir "posts" "*.md")}}
  
{{markdown (read this)}}
{{/each}}