0

Golang server is sending schools object to print.tpl smarty file like: tplData["Schools"] = schools

In print.tpl file, I am able to print it using below:

{{range $.Schools}}
    {{.Course}} --  {{.Duration}}
{{end}}

In print.tpl file, I need to use https://fullcalendar.io JQuery component and it works fine with static data like below:

<script>
    $(document).ready(function() {
         $('#calendar').fullCalendar({             
             header: {
                 left: 'prev,next today myCustomButton',
                 center: 'title',
                 right: 'month,agendaWeek,agendaDay,listMonth'
             },
             events: [
                  {
                      title  : 'event1',
                      start  : '2017-08-01'
                  }
              ]
         });
    });
</script>

Question: How can I iterate $.Schools object in my JQuery function?

Note: Hosting REST in Golang and calling in JQuery is an option, but I don't want to go that route.


Update: New enhanced code as per @mkopriva brilliant answer:

<script>
        $(document).ready(function() {
             $('#calendar').fullCalendar({             
                 header: {
                     left: 'prev,next today myCustomButton',
                     center: 'title',
                     right: 'month,agendaWeek,agendaDay,listMonth'
                 },
                 events: [
                      {{range $.Schools}}
                      {
                        title  : {{.Course}},
                        start  : {{.Duration}}
                      },
                  {{end}}
                  ]
             });
        });
    </script>
Nikhil Joshi
  • 817
  • 2
  • 12
  • 34

1 Answers1

2

Go templates support js and css and the evaluation of actions ({{ ... }}) is contextual so you can iterate over schools in js the same as you do in html. Although I have no idea what smarty is, so if this isn't working you need to check smarty's documentation.

events: [
    {{range $.Schools}}
    {
        title: {{.Course}},
        start: {{.Duration}}
    },
    {{end}}
]

https://play.golang.org/p/LBDBAMY4cL

mkopriva
  • 35,176
  • 4
  • 57
  • 71
  • Excellent @mkopriva, I just have one small issue, with above code I end up adding additional COMMA at the end. Do you know how can I identify whether last element during iteration, so that I will not add that COMMA and everything will be great? – Nikhil Joshi Aug 26 '17 at 15:18
  • @NikhilJoshi One way is to use a custom template function like here https://stackoverflow.com/questions/22367337/last-item-in-a-golang-template-range mainly this example https://play.golang.org/p/V94BPN0uKD. Or you can, in Go, construct the school slice as a `[]map[string]interface{}` where the keys are fromatted the same as they need to be in the js calendar and then just drop that in as is, without `range` and Go template will create a js array of objects from that slice of maps... – mkopriva Aug 26 '17 at 15:29
  • ... maybe that's what @mh-cbon was trying to point out, who knows. Anyway here's an example: https://play.golang.org/p/z-Pqnce_bq – mkopriva Aug 26 '17 at 15:30
  • You are doing it in GO file, but mine is "print.tpl" file, see UPDATE in the original post. Sorry but I am a newbie in these languages, please suggest how can I get rid of the last COMMA inside "print.tpl" file? – Nikhil Joshi Aug 26 '17 at 15:37
  • @mkopriva when a value is printed within a script tag, it automatically prints json. so you just put `var ...` in front of it to consume it in the js. assuming $.Schools is an array of map/struct, `var event = {{$.Schools}}`. –  Aug 26 '17 at 15:45
  • @NikhilJoshi If you're using the `html/template` package to render the `print.tpl` file you should be able to do all of the things in the provided examples, it doesn't matter if the file extension is `.go`, `.tpl`, `.tmpl`, `.html`, `.js` or what have you. If you're using some other package to render the file, then you should mention that and provide a link to it in the original question. – mkopriva Aug 26 '17 at 16:03
  • I am using ".tpl" extension, but you saved me, thanks a lot, accepting your answer! – Nikhil Joshi Aug 26 '17 at 16:08
  • Just for the record, [Smarty](https://www.smarty.net/) is a famous PHP templating system, dating from the turn of the century and still being under continuous development & maintenance, which is well-known for being super-fast. The forum software phpBB used to be based on Smarty templates, and, to an extent, so does Go — it's definitely _inspired_ on Smarty, even though, because of the differences between Go and PHP, Go templates are quite different. I assume that the OP mentioned 'smarty' because he's familiar with `.tpl` files used by Smarty. – Gwyneth Llewelyn Jun 21 '20 at 19:22