Content Hashing in Templates

The get_hash() template function computes SHA hashes of files or literal strings directly in your templates. Use it for query-parameter cache busting, SRI integrity attributes, or any scenario that needs a content-derived identifier.

Quick Start

{# Cache-bust a stylesheet with a query parameter #}
<link rel="stylesheet" href="/theme/assets/css/style.css?v={{ get_hash(path='theme/assets/css/style.css') }}">
{# Renders: /theme/assets/css/style.css?v=a1b2c3d4e5 #}

Parameters

ParameterTypeDefaultDescription
pathstringFile path relative to theme assets or site root
literalstringString to hash directly (no file I/O)
sha_typeint256Hash algorithm: 256, 384, or 512
fullboolfalseReturn full base64-encoded hash (SRI format)
lengthint10Hex prefix length when full=false

Exactly one of path or literal must be provided.

File Hashing

Hash a file in the theme assets directory:

<link rel="stylesheet"
      href="/theme/assets/css/style.css?v={{ get_hash(path='theme/assets/css/style.css') }}">

File resolution order:

  • Build mode: Output directory (after asset copy and Sass compilation), then theme assets, then theme root
  • Serve mode: Theme assets directory, then theme root, then media directory

If the file is not found, get_hash returns an empty string and logs a warning.

Subresource Integrity (SRI)

Use full=true to get a base64-encoded hash suitable for the HTML integrity attribute:

{% set hash = get_hash(path='theme/assets/js/app.js', sha_type=384, full=true) %}
<script src="/theme/assets/js/app.js"
        integrity="sha384-{{ hash }}"
        crossorigin="anonymous"></script>

String Hashing

Hash a literal string (no file I/O):

{# Generate a cache key from content #}
{% set cache_key = get_hash(literal=page.content) %}

{# Hash a version string #}
{% set build_id = get_hash(literal=accent.version ~ accent.git_hash) %}

Algorithm Selection

{# SHA-256 (default) #}
{{ get_hash(literal="hello", sha_type=256) }}

{# SHA-384 #}
{{ get_hash(literal="hello", sha_type=384) }}

{# SHA-512 #}
{{ get_hash(literal="hello", sha_type=512) }}

Custom Length

By default, the hex prefix is 10 characters. Adjust with length:

{# Short 6-character hash #}
{{ get_hash(literal="hello", length=6) }}

{# Longer 16-character hash #}
{{ get_hash(literal="hello", length=16) }}

Caching

File hashes are cached in memory to avoid redundant disk I/O:

  • Build mode: Cache lives for the duration of the build. Same file hashed in multiple templates is read only once.
  • Serve mode: Cache persists until a theme hot reload (SIGHUP or admin reload), which clears the cache so updated assets produce new hashes.

String hashing (literal=) is not cached since it involves no I/O.

Relationship to Asset Fingerprinting

get_hash() is the low-level primitive. The | fingerprint filter is the higher-level automation that renames files with content hashes. Use get_hash when you need:

  • Query-parameter cache busting (?v=hash) instead of filename rewriting
  • SRI integrity attributes
  • Content-derived identifiers for any purpose
  • Hashing strings rather than files

Edition Availability

The get_hash() function is available with no license key.