`function "CONTENT" not defined` with templates and Markdown

My version of Caddy is 0.8.3, which is up to date as of the writing of this post (June 26, 2016).

I’m trying to use the markdown directive with custom templates and am getting this error in my error logs:

26/Jun/2016:20:53:34 -0400 [ERROR 500 /markdown/index.md] template: :77: function "CONTENT" not defined

Here’s the relevant portion of my Caddyfile.

sitename.com {
        root www/sitename.com
        errors sitename.com.errors.log
        markdown /markdown {
                template template.html
        }
}

The error occurs as long as any file named index.md is present in the /markdown directory, which is to say that the same error is reported regardless of the contents of template.html, whether it exists or not.

The error only occurs when Caddy attempts to load the template (I believe). By replacing template template.html in the Caddyfile with template default template.html (loading a template named default rather than replacing the default template) the default default template is used and no error occurs.

The contents of template.html are, as previously stated, entirely irrelevant, but here are two potential configurations that produce the same error:


(a blank file)

And:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>{{.Doc.title}}</title>
		<link rel="stylesheet" href="markdown.css">
	</head>
	<body>
		<div id="container">
			{{.Doc.body}}
		</div>
	</body>
</html>

Which is the template I’d like to use.

Thanks for the help and let me know if there’s anything I missed!

And shoutout to Matt Holt for making an awesome server! :sunglasses:

Thanks for your compliment!

What’s your index.md file look like, and what is template.html? Somewhere must be the word “CONTENT”.

Somewhere must be the word “CONTENT”.

Unfortunately not.

Currently, index.md contains:

hi

and template.html contains:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>{{.Doc.title}}</title>
		<link rel="stylesheet" href="markdown.css">
	</head>
	<body>
		<div id="container">
			{{.Doc.body}}
		</div>
	</body>
</html>

Both are encoded in UTF-8 with Unix line-endings.

Searched around in the source a bit and I think the error might be being thrown from something in here (lines 46–64 in caddyhttp/templates/templates.go)

// Build the template
templatePath := filepath.Join(t.Root, fpath)
tpl, err := tpl.ParseFiles(templatePath)
if err != nil {
	if os.IsNotExist(err) {
		return http.StatusNotFound, nil
	} else if os.IsPermission(err) {
		return http.StatusForbidden, nil
	}
	return http.StatusInternalServerError, err
}

// Execute it
var buf bytes.Buffer
err = tpl.Execute(&buf, ctx)
if err != nil {
	return http.StatusInternalServerError, err
}

Which seems to suggest the error is being thrown from the text/template package. I’ll dig around and see if I can find anything.

Update: It looks like it’s being thrown by line 668 in text/template/parse/parse.go from func (t *Tree) term Node.

But you’re using markdown, not templates. I did a fulltext search on the Caddy source, the Go std lib source, and blackfriday (our Markdown renderer) source, and no results for “CONTENT” as an identifier of any sort… :sweat:

But you’re using markdown, not templates.

The error only occurs when using markdown with a custom template, though — without trying to use a custom template, the markdown renders fine.

Update: Using the templates directive in my Caddyfile works fine (templates /templates .html).

For reference, the templates/ directory is laid out like this:

sitename.com
└templates/
 ├index.html
 └sample.md

However, when I tried to include a markdown file in index.html with {{.Markdown "sample.md"}} it produced a 500 error in my errors log (linebreaks added for readability):

27/Jun/2016:18:02:48 -0400 [ERROR 500 /templates/]
template: index.html:9:5: executing "index.html" at <.Markdown>:
error calling Markdown: open www/becca.ooo/sample.md: no such file or directory

which was rectified when I changed the markdown inclusion to {{.Markdown "templates/sample.md"}} — so I guess paths in templates are treated as absolute relative to the site base path?

Anyways, the bit to glean here is that Go template errors are structured as template: filename:linenumber:columnnumber errortext, meaning that my previous error (template: :77: function "CONTENT" not defined) occured in line 77 of an unknown file at an unknown column. I’m not sure how helpful that is, though…

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