.Markdown and .Include templates not working inside a {{range}} block?

1. My Caddy version (caddy version):

Version 1.0.4

2. How I run Caddy:

I’m setting up a small blog. Directory structure looks like this:

- orange.css # my CSS
- index.html # this is where the problem is happening
- blogpost.html # a template for my blogposts
- /blog # the blogpost .md files
+ - markdowntest.md # an example .md file

a. System environment:

I’m running Arch on a VPS, Linux kernel 4.9, systemd 244.1-1.

b. Command:

cd /path/to/my/htmldir
sudo caddy

c. Service/unit/compose file:

I do not know where to look for these files, and haven’t set them up.

d. My complete Caddyfile or JSON config:

My caddyfile is:


markdown /blog {
        css /orange.css
        template blogpost.html


3. The problem I’m having:

I am trying to generate a list of files in /blog, linked to those files, with the contents of the file afterward–basically, an index for my blog posts. However, when I try to .Include or .Markdown a file inside of the range I’m using to list all the files, I get a 500 internal server error.

Here’s the templating I’m doing in index.html:

<div class="title">Blog entries:</div>
<div class="entry">
{{.Include "blog/markdowntest.md"}} <!-- .Include and .Markdown both work here... -->
{{range $field := .Files "blog"}}
{{ $path := "blog/" }}
<a href="{{$path}}{{$field}}"></a> - {{.Include "blog/markdowntest.md"}} <!-- But using a .Include here causes a 500 Internal Server Error when I load the page. -->

In addition, trying to do any kind of string concatenation–such as {{$path := "blog/" + $field}}–also causes a 500 Internal Server Error.

4. Error messages and/or full log output:

Caddy starts without issues, outside of an inode complaint:

ns507181% sudo caddy
[sudo] password for dbrz:
Activating privacy features... done.

Serving HTTP on port 80

Serving HTTPS on port 443

WARNING: File descriptor limit 1024 is too low for production servers. At least 8192 is recommended. Fix with `ulimit -n 8192`.

There is no additional output after I attempt to load index.html.

Running with -log log.txt provides the following output in log.txt:

ns507181% cat log.txt
2020/04/12 01:38:23 [INFO] Caddy version: v1.0.4
2020/04/12 01:38:23 [INFO][cache:0xc0001a2b40] Started certificate maintenance routine
2020/04/12 01:38:23 [INFO] Serving https://lambda-delta.tk
2020/04/12 01:38:23 [INFO] Serving http://lambda-delta.tk
2020/04/12 01:40:33 [INFO] SIGINT: Shutting down
2020/04/12 01:40:33 [INFO][cache:0xc0001a2b40] Stopped certificate maintenance routine

Again, nothing changes after I try to load the page. No additional output.

5. What I already tried:

I’ve narrowed it down to concatenation or running an .Include or .Markdown inside the range. I’ve gotten it working without the “preview”, and can make a list of links that go where they’re supposed to, but if I try to do anything further than that it falls apart.

6. Links to relevant resources:

Caddy V1 template actions documentation:
Go Template docs:

Update: I found a way to do string concatenation using printf:
{{$path := printf "%s%s" "blog/" $res}}

I’m also experimenting with with, but so far .Markdown and .Include still fail, even if I call them as {{with printf %q $path | .Markdown .}}.

Further research: Using variables in .Markdown appears to break. Which is rather annoying.

In what way? You might try upgrading to Caddy 2, which has a number of improvements. (I’m just suggesting this generally – without knowing more details it’s impossible to know how to be helpful toward a solution to your problem though.)

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