caddy: Can't access object created in markdown front matter
1. What version of Caddy are you running (caddy -version)?
8.3, But it doesn’t matter. It’s the same in 8.2 also.
2. What are you trying to do?
Access an object in markdown front matter:
In template:
<p>{{ .Doc.someobject.somefield }}</p>
In front matter (TOML, but the same in all front matter):
+++
title = "sometitle"
[someobject]
somefield = "somevalue"
+++
3. How did you run Caddy (give the full command and describe the execution environment)?
Run on mac osx, with just caddy
4. What did you expect to see?
On the page, <p>somevalue</p>
5. What did you see instead (give full error messages and/or log)?
Nothing at all, no error, and nothing on the page. Inspecting the page reveals an element <no value> put in place instead of the actual value.
6. To reproduce this bug:
Create a Caddyfile with a markdown directive, the options don’t matter, just make it so that it will read files in the markdown directory as markdown files and the file template.html as the default template.
Create a directory markdown and create a markdown file in the directory, call it index.md.
Put the following in the file index.md:
+++
key = "value"
[object]
objkey = "objvalue"
+++
Page
Create the file template.html and put the following in it:
<!doctype html>
<html lang="en">
<head>
<title>Title</title>
</head>
<body>
Key: {{ .Doc.key }}
Objkey: {{ .Doc.object.objkey }}
</body>
</html>
Now, start caddy and go to <your-caddy-server-url>/markdown/index.md in a browser.
You will see:
Key: value
Objkey:
The desired result is:
Key: value
Objkey: objvalue
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 16 (4 by maintainers)
Okay, I’ll do that now.
That looks fine.
If you are willing to contribute that, add/edit a test case or two to include objects for json, toml, yaml. We’ll be glad to merge it 😃
My solution was the use of interfaces, it wasn’t very complicated. You can either add another variable alongside Flags, and Variables in metadata.go and in process.go, so you could use it like
{{ .DocObjects.<someobject> }}or whatever you want to call it and then set adefault:in metadata.load in the parsedMap for loop. Ex: (Around line 50 in markdown.go)Or you can just change Variables (in markdown.go) and Doc (in process.go) to
map[string]interface{}and then around line 190 in process.go change[]byte(title)to[]byte(title.(string))and then{{ .Doc.<someobject>will just work. Ex: (Around line 50 in markdown.go)Take look at https://github.com/mholt/caddy/compare/master...MrH100000:master.
@MrH100000 Is it complicated? We could take another look. Mostly we were trying to avoid complexity.