Define custom templates

1. Caddy version (caddy version):

2.0

2. How I run Caddy:

local via brew install

a. System environment:

MacOS 10.15

b. Command:

caddy start

c. Service/unit/compose file:

paste full file contents here

d. My complete Caddyfile or JSON config:

root * src
file_server
templates
encode gzip

try_files {path}.html {path}

3. The problem I’m having:

I couldn’t find documentation on how to configure which html file to use for a template defined in Front Matter of a markdown file.

4. Error messages and/or full log output:

5. What I already tried:

6. Links to relevant resources:

Appears it was doable in v1 https://caddyserver.com/v1/docs/markdown

I’m not sure what your question is exactly.

I think you could use the Caddy website’s source as inspiration:

Take a look at the Caddyfile, you’ll notice that it has a rewrite to map everything in certain sections of the site (everything in a subpath) to specific index files.

Then you can take a look at the src/docs/index.html to see how front matter is parsed and how markdown content is included into the page.

I understand basic key/value pairs in Front Matter. I’m coming from Jekyll land.

The v1 docs at the bottom

With custom templates:

markdown /blog {
	template default.html
	template blog  blog.html
	template about about.html
}

that doesn’t work in v2 correct? Is there a replacement?

Hey Michael,

In v1, we determined that the markdown directive wasn’t very useful. The idea from that was that it served static Markdown files by rendering them as HTML. But Markdown without styling wasn’t very useful, either, so we added css and js and eventually template subdirectives so you could make static Markdown files presentable.

… but that’s the same thing as the templates directive.

So we got rid of the markdown directive.

Now in v2, you’d use the templates directive like we do for the Caddy site here:

That directive renders response bodies (for example, static files loaded by file_server) as templates. Our Caddyfile rewrites all requests in /docs/* to go to an HTML file that is rendered as a template. It starts with these lines:

Those lines load the actual requested markdown file and parse out its front matter from its body.

Then we can just render it as markdown in the proper place in the HTML:

So basically, the static files are the template(s).

This is just an example but that’s how you use it.

3 Likes

Thanks for the detailed explanation. I followed the logic in the example site, but clarifying that indeed that feature has been removed was what I needed.

I was specifically drawn by the markdown directive and not sure I want to do all of the conditional logic in a single html file if I want to use more than one layout in a single directory.

Otherwise, I love the idea of skipping the build step.

You can still adjust this in Caddy 2, just do your rewrites differently.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.