Golang + JQuery + Smarty:如何遍历对象?

huangapple go评论117阅读模式
英文:

Golang + JQuery + Smarty: How to iterate over object?

问题

Golang服务器正在将schools对象发送到print.tpl Smarty文件,如下所示:tplData["Schools"] = schools

print.tpl文件中,我可以使用以下代码打印它:

  1. {{range $.Schools}}
  2. {{.Course}} -- {{.Duration}}
  3. {{end}}

print.tpl文件中,我需要使用https://fullcalendar.io JQuery组件,并且它可以与以下静态数据正常工作:

  1. <script>
  2. $(document).ready(function() {
  3. $('#calendar').fullCalendar({
  4. header: {
  5. left: 'prev,next today myCustomButton',
  6. center: 'title',
  7. right: 'month,agendaWeek,agendaDay,listMonth'
  8. },
  9. events: [
  10. {
  11. title : 'event1',
  12. start : '2017-08-01'
  13. }
  14. ]
  15. });
  16. });
  17. </script>

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

注意:在Golang中托管REST并在JQuery中调用是一种选择,但我不想选择这种方法。


更新:根据@mkopriva的出色答案,更新了增强代码:

  1. <script>
  2. $(document).ready(function() {
  3. $('#calendar').fullCalendar({
  4. header: {
  5. left: 'prev,next today myCustomButton',
  6. center: 'title',
  7. right: 'month,agendaWeek,agendaDay,listMonth'
  8. },
  9. events: [
  10. {{range $.Schools}}
  11. {
  12. title : {{.Course}},
  13. start : {{.Duration}}
  14. },
  15. {{end}}
  16. ]
  17. });
  18. });
  19. </script>
英文:

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

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

  1. {{range $.Schools}}
  2. {{.Course}} -- {{.Duration}}
  3. {{end}}

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

  1. &lt;script&gt;
  2. $(document).ready(function() {
  3. $(&#39;#calendar&#39;).fullCalendar({
  4. header: {
  5. left: &#39;prev,next today myCustomButton&#39;,
  6. center: &#39;title&#39;,
  7. right: &#39;month,agendaWeek,agendaDay,listMonth&#39;
  8. },
  9. events: [
  10. {
  11. title : &#39;event1&#39;,
  12. start : &#39;2017-08-01&#39;
  13. }
  14. ]
  15. });
  16. });
  17. &lt;/script&gt;

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:

  1. &lt;script&gt;
  2. $(document).ready(function() {
  3. $(&#39;#calendar&#39;).fullCalendar({
  4. header: {
  5. left: &#39;prev,next today myCustomButton&#39;,
  6. center: &#39;title&#39;,
  7. right: &#39;month,agendaWeek,agendaDay,listMonth&#39;
  8. },
  9. events: [
  10. {{range $.Schools}}
  11. {
  12. title : {{.Course}},
  13. start : {{.Duration}}
  14. },
  15. {{end}}
  16. ]
  17. });
  18. });
  19. &lt;/script&gt;

答案1

得分: 2

Go模板支持js和css,并且actions{{ ... }})的评估是有上下文的,所以你可以像在html中一样在js中迭代学校。虽然我不知道smarty是什么,所以如果这不起作用,你需要查看smarty的文档。

  1. events: [
  2. {{range $.Schools}}
  3. {
  4. title: {{.Course}},
  5. start: {{.Duration}}
  6. },
  7. {{end}}
  8. ]

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

英文:

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.

  1. events: [
  2. {{range $.Schools}}
  3. {
  4. title: {{.Course}},
  5. start: {{.Duration}}
  6. },
  7. {{end}}
  8. ]

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

huangapple
  • 本文由 发表于 2017年8月26日 22:43:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/45896605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定