-3

basically I have an object wanted to pass to the frontend. I logged it in the backend and it was not null, but in the frontend when I alerted it, it becomes null.

...
presentation := &presentationStruct {
  Object: object,
}
log.Errorf("%v", object) // not null
template.Execute(writer, presentation)
...

// but it becomes null here
alert({{.Object}})

The object is a type of

map[string]map[string]struct {
  []float32
  map[int][]struct {
    string
    float32
  }
}

Is it because the type is too complicated?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Haha
  • 31
  • 6
  • You're not supposed to leave `{{ .Object }}` it's not magical. You need, in your template, to do operations on it, cycle through your map, so you can render actual text and not a structure. You can for example render something like this `{{ .Object['foo']['bar'][0] }}` (This is just an example, you need to access the data that is contained IN your object) – Depado Jun 23 '15 at 14:03
  • @ Depado I know that I should render an actual text, but if the object is null, how do you refer anything – Haha Jun 23 '15 at 18:55
  • @ Dave C see this example http://play.golang.org/p/s8hOuzI-3F – Haha Jun 23 '15 at 18:57

1 Answers1

0

No, it does not matter that the type is complicated. Template is not for passing struct from backend in Go to frontend in JavaScript. It is just do string replacement, replace strings in template with variables in Go and send final string.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Jiang YD
  • 3,205
  • 1
  • 14
  • 20
  • The abstraction can do more than just map strings to strings... It wouldn't provide much value if that's all it did. Why bother with the library at all if does nothing more than a good old string.replace? – evanmcdonnal Jun 23 '15 at 21:36
  • replacement, not means replace ONE string to ANOTHER, like string.replace does. Maybe, template is for string generation, saying better. – Jiang YD Jun 24 '15 at 00:58
  • @Haha you say in the example, everywhere you want to generate a string in place, using {{. xxx }}, the xxx is a primitive value, not a object(struct in golang). – Jiang YD Jun 24 '15 at 01:02
  • In a world of your "replacement", even compilers can only do "string replacement". Clearly "replacement" is too vague to explain what happens. – Haha Jun 24 '15 at 02:23
  • Can you explain this code in your theory of replacement: var values = {{.Object}}[key]; – Haha Jun 24 '15 at 02:34
  • `{{.Object}}[key]` presents as a primitive value. if it print in final html – Jiang YD Jun 24 '15 at 06:14
  • I really mean that you need place a primitive value on where you need them be resolved in final html. no more chat, please. – Jiang YD Jun 24 '15 at 06:16
  • @Jiang YD Stop finding excuse for your naive anwers. Of source it should be a primitive value eventually. But clearly you don't know that {{.Object}} is a json object. – Haha Jul 31 '15 at 22:09