Indenting in Go templates

agolangf · · 686 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<pre><code>{{define A}} D E F {{end}} A B C {{template A}} </code></pre> <p>Let&#39;s say I have the template setup above. How do I make &#34;template A&#34; stay properly indented on rendering? I saw someone suggest piping {{template A | indent 7}} , where indent is some custom function inserting 7 spaces or similar on each line.</p> <p>Any other suggestions, or is this &#34;the&#34; way?</p> <hr/>**评论:**<br/><br/>epiris: <pre><p>You can use hypen to trim space before and after templates <a href="https://github.com/cstockton/go-conv/blob/927c0d2da3e0a9499ac27d1f078a82437dca2dce/testdata/numeric.go.tpl" rel="nofollow">like this</a> to have proper indentation. It&#39;s a bit tedious at first but once it clicks it&#39;s no big deal.</p></pre>Handofmidasau: <pre><p>Doesn&#39;t help in this case</p></pre>epiris: <pre><p>Example includes solutions, mix usage of hyphen and Params. Pass something called indent or spacer through pipeline inc or dec it as you enter or exit new pipelines. Have a helper func to dec() and inc(). There&#39;s a hundred ways to do it.</p></pre>titpetric: <pre><p>Shit, this example is fucked up. Most likely I&#39;d just go fmt the output because shit, it&#39;s taxing enough to write in a meta-language to generate code source, without having to pay attention to indentation as well.</p></pre>ericzhill: <pre><p>This bugs the heck out of me too. I hacked together a pretty print function using Antlr4 just so I could make my templates sane after parsing.</p></pre>mixedCase_: <pre><p>Embrace the OCD.</p></pre>jerf: <pre><p>&#34;It depends&#34;. Is this HTML, despite what you&#39;re showing us? Then really the best thing is just to stop caring. Is it text? Is it <em>really</em> text? If it&#39;s some actual format you may be better off writing something that creates an AST for the format (or using one that already exists), and rendering it at the end. What sounds initially like more work very often pays off as less work total in the end.</p> <p>But failing all that, yeah, the indent solution seems pretty good.</p></pre>Handofmidasau: <pre><p>no, it is xml. But sometimes you want to present the output in a proper manner. Sure it &#34;does not matter&#34;, except when it does.</p></pre>jerf: <pre><p>You may be better off then post-processing with an xml prettifier, then. Be as gross as you want during output and let the prettifier fix it all in one shot.</p> <p>Depending on how many templates you have this may be both easier and ultimately more effective. Prettifiers often also come with tunable parameters, which are much easier to tune at that level than to modify every template.</p> <p>I also suggest looking at encoding/xml. I absolutely, positively do NOT guarantee it will meet your use cases, because it is a library that chooses some ease-of-use over total flexibility. But if it so happens it can meet your needs, it may be able to completely eliminate manipulating XML templates <em>at all</em>, in favor of using <a href="http://127.0.0.1:9000/pkg/encoding/xml/#MarshalIndent" rel="nofollow">MarshalIndent</a>. Basically, in the case of XML, not only does the AST exist but there&#39;s a translation layer between Go structs and that AST in the standard library.</p> <p>I offer these in the spirit of giving helpful suggestions to think about in the hopes that one of them may help, rather than &#34;encouraging&#34; you to choose any of them.</p></pre>

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

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