Assign to javascript var in template

I want to do something alone these lines:

var videos = {{.Split .Doc.media ","}};

where media is a comma-separated string of names that should become the videos array. I can’t seem to figure out a way to accomplish this. I’ve tried using a YAML list as well as a string.

I was able to do it with:

var videos = '{{js .Doc.media}}'.split(',');

But if anyone knows how I can do this with a YAML list, please chime in. I’d prefer not to do the split().

What result was that giving you? (the first attempt) - I doubt it produces JavaScript code…

The first attempt produces nothing. Second attempt works, but I’d like to be able to use a YAML array.

It works.

Caddyfile:

localhost
markdown {
    template tpl.html
}

index.md:

---
media: "foo,bar"
---

Testing!

tpl.html:

<html>
<body>
	{{range .Split .Doc.media ","}}
		{{.}}<br>
	{{end}}
</body>
</html>

Output:

foo
bar

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