Can you get ERB to properly indent when rendered?

徘徊边缘 提交于 2020-06-10 13:21:51

问题


I have several partials that I'm including in my Rails application.html.erb file, but the resulting HTML doesn't preserve my indenting (formatting). I've been told that the first line gets rendered with the same indentation-level as the call to _partial.html.erb, but all subsequent lines in the partial just get left-aligned.

This results in code like (see my comments for positioning):

<body>
    <div id="outer">

        <div class="contentwidth main">
            <div class="logo"> <<<<< Shouldn't be this far to the right
    <h1><a href="index.html">minimal.</a></h1>
</div><!-- end logo -->     <<<<<<< Shouldn't be way over to the left

Is there any way to fix this/format my included partials better using ERB? Or do I need to use HAML?


回答1:


The doc on filters contains an example of using an after filter to compress the html before sending it to the browser.

Doing something similar, but using something like Tidy to reformat and replace the html where this example does a compress might do the trick.




回答2:


Not the answer you want - but - no, there's nothing built into Rails to auto-indent ERB.



来源:https://stackoverflow.com/questions/5691653/can-you-get-erb-to-properly-indent-when-rendered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!