User Guide

http.templates

templates allows you to add template actions to your pages. Templates can be a convenient way to render the current timestamp, request URL, visitor's IP address, and more. You also get basic control statements like if and range. See Template Actions for instructions on how to use templates.

Some common uses of templates are to include the content of other files, show the current date or time, and hide or show certain parts of a page depending on the request path, cookies, or headers.

Note that custom error pages do not get executed as templates, even if this directive is enabled. Error pages are served by a separate middleware.

Templates can come from static files or be loaded by other middleware. For example, you can proxy to a backend that outputs a template which Caddy then executes with this directive.

Syntax

templates [path [extensions...]]

To specify certain extensions, a path must also be provided. The default path is / and the default extensions that will be executed as templates are .html, .htm, .tpl, .tmpl, and .txt.

For more options, open a block:

templates { path basepath ext extensions... between open_delim close_delim }

Template Format

See Template Actions.

Example Template File

<!DOCTYPE html>
<html>
	<head>
		<title>Example Templated File</title>
	</head>
	<body>
		{{.Include "/includes/header.html"}}
		<p>
			Welcome {{.IP}}! You're visiting {{.URI}}.
		</p>
		{{.Include "/includes/footer.html"}}
	</body>
</html>

Examples

Enable templates for all .html, .htm, .tpl, .tmpl, and .txt files:

templates

Templates for the same file extensions but only under /portfolio:

templates /portfolio

Enable templates only on .html and .txt files in /portfolio:

templates /portfolio .html .txt

Need help with this feature?

Join us in the Caddy forum, where the open source community gathers to share their knowledge.

Does your company use Caddy?

Purchase a commercial license to use Caddy with your business and for basic email support, along with other benefits!