2017-08-26 40 views
0

Golang服务器发送schools对象print.tpl Smarty的文件,如:tplData["Schools"] = schoolsGolang + JQuery + Smarty:如何迭代对象?

print.tpl文件,我用下面我能够打印:

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

print.tpl文件,我需要使用https://fullcalendar.io JQuery的组件并与静态数据工作正常,如下图所示:

<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> 

问题:如何在我的JQuery函数中迭代$.Schools对象?

注意:在Golang托管REST和调用jQuery是一种选择,但我不希望走这条路。


更新:新的增强的代码按@mkopriva辉煌答案:

<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> 

回答

1

转到模板支持JS和CSS和行为评价{{ ... }})为背景,所以你可以遍历js中的学校与html中的相同。虽然我不知道smarty是什么,所以如果这不起作用,你需要检查smarty的文档。

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

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

+0

https://play.golang.org/p/_jJzH28bOI –

+0

@ MH-cbon它是什么,你要指出的? – mkopriva

+0

优秀的@mkopriva,我只有一个小问题,上面的代码我最终在最后添加了额外的COMMA。你知道我怎样才能确定是否在迭代过程中的最后一个元素,以便我不会添加该COMMA,一切都会很好? –