<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 "Embedded RuBy", so in the general case, I woudn't expect an erb template to be executable in anything but Ruby. You'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 "your" because it is not a solution for running "erb templates", 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's a lot of false positives in there because this is hard to search for, but there'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'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'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're looking at a <em>super</em> high performance website, this is probably fine.</li>
</ol>
<p>There's also a hybrid solution where you start with #3 to get going quickly, but rewrite the important ones incrementally.</p>
<p>I'd also observe this is not "Go's" 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's html/template in a lot of ways, there'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'd even be able to convert your templates into Mustache, make various modifications to them, and then decide you're going to stick to Ruby, or switch to some other language, and you wouldn'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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传