Can I use templates dynamically?

xuanbao · · 1645 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Is there a way I can use go&#39;s templating engine, but somehow have the contents be updated dynamically? For example, if there is a list of items (a sample of 10/1000) and then a user enters a search term, it send this query to the server which then filters the items and returns the new 10 to be displayed.</p> <p>The only way I can think about doing it is having some UI framework like react, and then having a websocket to the server to communicate the results, but this seems over engineered.</p> <hr/>**评论:**<br/><br/>Midnightblues: <pre><p>This is totally doable, and quite easily. In the server simply construct a list of results, and in your template range over the list you constructed. This will let your template handle any number of results. </p> <p>No JS or web sockets necessary. </p></pre>Elusiveblade: <pre><p>Thank you for your reply, but what I&#39;m aiming for is a little different.</p> <p>I am aiming to have a search box on a page, where as you enter your search term, the results below update. Here, your strategy works for populating the results before we begin, but my problem is with updating this list once we start typing.</p></pre>mixedCase_: <pre><blockquote> <p>I am aiming to have a search box on a page, where as you enter your search term, the results below update.</p> </blockquote> <p>Then you&#39;re looking at doing that with JavaScript on the site itself, using AJAX to call to the Go server&#39;s API for searching stuff and then loading the results to the DOM.</p> <p>This is what things like the React framework are designed for. Although there are about a couple million different frameworks to do the same thing. The JavaScript ecosystem is a <em>little</em> fragmented in that regard.</p></pre>SportingSnow21: <pre><p>Two basic options are:</p> <ul> <li>Use Angular/React/etc to develop a single-page app that communicates via JSON/XML with the back-end.<br/></li> <li>Use the templating engine to generate the page from the user query. There&#39;s a range construct that will loop over a slice of results to display each item. {{_,v:= range slice}} use the slice element {{end}}</li> </ul> <p>If there&#39;s not a lot of interaction on the page, a JS framework can be overkill. </p></pre>rvxt: <pre><p>I&#39;d recommend against the JSON powered SPA architecture that&#39;s all the rage these days. Instead, I&#39;d recommend getting a good grip of the templating system and doing full round trips to the server.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1645 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传