I'm trying to create a toc.ncx (just an XML) file for an epub.
In such a toc, <navpoint>
s are defined, each one having an id and a "playorder".
Is there any way for a Go template to simply count the navpoints?
I thought about something like this (non-working template code)
{{$counter := 0}}
{{define "NAVPOINT"}}
{{$counter = $counter + 1}}
<navpoint id="{{.}}" playorder="{{$counter}}">
{{end}}
{{range .TopLevel }}
{{template "NAVPOINT" .Id}}
{{range .SecondLevel }}
{{template "NAVPOINT" .Id}}
</navpoint>
{{end}}
</navpoint>
{{end}}
Example Structure
{ TopLevel: [
{
Id: "id-a"
SecondLevel: [
{
Id: "scnd-a"
}
{
Id: "scnd-b"
}
]
}
{
Id: "id-b"
SecondLevel: [
{
Id: "scnd-c"
}
{
Id: "scnd-d"
}
]
}
}
This would then give me something like this
<navpoint id="id-a" playorder="1">
<navpoint id="scnd-a" playorder="2"></navpoint>
<navpoint id="scnd-b" playorder="3"></navpoint>
</navpoint>
<navpoint id="id-b" playorder="4">
<navpoint id="scnd-c" playorder="5"></navpoint>
<navpoint id="scnd-d" playorder="6"></navpoint>
</navpoint>
I think I can use a template function, not a template to achieve this, but have no clue how to achieve this. Can a template function have a counter?