Thomas Sundberg

June 19, 2015

Include header and footer in a Mustache template

This post has been migrated to

A template for creating web pages is great if you want to integrate front- and backend easy. I have been using Mustache for a while and I’m quite satisfied. It works great with both Dropwizard and Spark

The documentation says how to include a snippet from another template. But searching for it was not that easy.

The problem I would like to solve is to reuse the same header and footer on many web pages. That is, include a snippet from another template into a template.

The solution is to refer to a another template from a template using {{> other_template }} in it.

Given this template:


{{> header}} <p>Page content</p> {{> footer}}

This header:


<!DOCTYPE HTML> <html> <head> <link href="" rel="stylesheet"> </head>

And this footer:


The header.mustache and footer.mustache will be included in index.mustache The result is a web page like this:


<!DOCTYPE HTML> <html> <head> <link href="" rel="stylesheet"> </head> <p>Page content</p> </body> </html>

This solves my problem with the same header and footer on each page using Mustache. Using the template system is different if you use Dropwizard, Spark or any other tool. But that is out of scope today.


I would like to thank Malin Ekholm and Johan Helmfrid for proof reading.


  • Mustache – Logic-less templates
  • Dropwizard – a Java framework for developing ops-friendly, high-performance, RESTful web services
  • Spark – a tiny Sinatra inspired framework for creating web applications in Java 8 with minimal effort
  • Thomas Sundberg – The author


  1. thanks very much🙂

    Comment by ly — February 5, 2016 @ 02:34

  2. You should append to the header template. Thanks!

    Comment by Johan — August 31, 2016 @ 11:47

    • I’m unsure what you mean by that.

      If I want to reuse the header template in many placese, how should I behave in that situation?


      Comment by Thomas Sundberg — August 31, 2016 @ 20:53

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: