ERB view embedded in a Haml layout: what to do about whitespace now?

元气小坏坏 提交于 2019-12-11 13:57:30

问题


I have a Rails 3 app where the views are all done in Haml. However, there are some views coming from a required gems that are in ERB, and Haml is adding whitespace, and it's messing up textareas and such. (If you're not familiar with this aspect of haml, look here.)

I'd rather not have to convert my layout to ERB, and because the gem is in ERB, all of the traditional Haml whitespace solutions don't help.

So how do I get my textareas (and <pre>s etc) not to wrap? Anything smart I can do in my app is good, otherwise I'd be happy to apply a patch to the original gem.


Edit I think I might have figured out a few decent answers.

One thing that works on the haml end is Haml::Template.options[:ugly] = true, which eliminates all whitespace. Meh, this might be the best solution, but where's my whitespace :( I liked it, when it wasn't messing up my app.

Another idea was to patch the gem to automatically convert \n characters to &#x000A; in a before_save, which mimics what Haml does in the link i linked to in my original question... but that might be a bit of a pain.

The final solution I came up with, which also seems to work, is to create a _form.html.haml document that sits right next to the _form.html.erb document in the plugin, and is identical. Apparently, the plugin doesn't need to depend on Haml, but if the parent app is rendering Haml, it'll look for a haml view before it looks for an ERB view. There's one caveat, though, and that's that Haml actually needs to be loaded at the beginning of the app. So I have a line at the bottom of application.rb now that just says 'Haml' all by itself.

So for anybody else having this problem, now you've got 3 solutions to pick from, and if you've got a better one, please post it!


回答1:


Have you looked at the :preserve filter?

See http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#preserve-filter



来源:https://stackoverflow.com/questions/5644926/erb-view-embedded-in-a-haml-layout-what-to-do-about-whitespace-now

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