Is there any method to use golang with erb-style template at run time

agolangf · · 469 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Just struggling with rewriting a ruby project with golang. The project need to get some erb-style content from database and render data to content at runtime. But go template is not compatible with erb-style</p> <hr/>**评论:**<br/><br/>jerf: <pre><p>Erb is itself an abbreviation of &#34;Embedded RuBy&#34;, so in the general case, I woudn&#39;t expect an erb template to be executable in anything but Ruby. You&#39;ve basically got three choices:</p> <ol> <li>If the templates are <em>really</em> stereotypical, you might be able to either write or modify an existing template solution to run <em>your</em> erb templates. I emphasize &#34;your&#34; because it is not a solution for running &#34;erb templates&#34;, but just your very specific ones. (Note Go does have <a href="https://godoc.org/?q=template" rel="nofollow">a number of template libraries</a>; there&#39;s a lot of false positives in there because this is hard to search for, but there&#39;s a Mustache renderer, some Go-only template solutions, etc.)</li> <li>You could rewrite the templates. This may not be as hard as you are thinking, though it will be non-trivial, because often you can do 80-90% of the work with some regexes. (The key here, in my opinion, is to write a program to do it, probably in Ruby, and don&#39;t modify the templates inline; have a clean input and output distinction so you can just modify the regexes and rerun them to see the results immediately. Sounds obvious when I say it, but it took me a while to learn this. It also means you don&#39;t have to worry about what happens if you run the regexes twice on the same file, etc.)</li> <li>You could run a Ruby server that just renders the templates. Unless you&#39;re looking at a <em>super</em> high performance website, this is probably fine.</li> </ol> <p>There&#39;s also a hybrid solution where you start with #3 to get going quickly, but rewrite the important ones incrementally.</p> <p>I&#39;d also observe this is not &#34;Go&#39;s&#34; fault per se; it is a generalized problem with using language-specific templating libraries. As much as I like a lot of the design decisions of Go&#39;s html/template in a lot of ways, there&#39;s a strong case to be made that you should almost always for any non-trivial project prefer to use something like Mustache templates that are widely cross-platform. In fact, you&#39;d even be able to convert your templates into Mustache, make various modifications to them, and then decide you&#39;re going to stick to Ruby, or switch to some other language, and you wouldn&#39;t lose your work. And html/template and text/template are definitely Go-specific, with some very Go-oriented behaviors in them.</p></pre>

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

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