嵌入 Haml 布局中的 ERB 视图:现在如何处理空白?

发布于 2024-11-01 02:58:15 字数 922 浏览 3 评论 0原文

我有一个 Rails 3 应用程序,其中的视图都是在 Haml 中完成的。然而,有一些视图来自 ERB 中必需的 gem,Haml 正在添加空白,这会弄乱文本区域等。 (如果您不熟悉 haml 的这方面,查看此处。)

我不想将我的布局转换为 ERB,并且因为 gem 位于 ERB 中,所以所有传统的 Haml 空白解决方案都没有帮助。

那么如何让我的文本区域(和

 等)不换行?我可以在我的应用程序中做的任何聪明的事情都是好的,否则我很乐意对原始 gem 应用补丁。


编辑我想我可能已经找到了一些不错的答案。

在 haml 端起作用的一件事是 Haml::Template.options[:ugly] = true,它消除了所有空格。嗯,这可能是最好的解决方案,但是我的空白在哪里:(我喜欢它,因为它不会弄乱我的应用程序。

另一个想法是修补 gem 以自动将 \n 字符转换为 
 在before_save,它模仿了 Haml 在我最初的问题中链接到的链接中所做的事情......但这可能有点痛苦

我想出的最终解决方案(似乎也有效)是创建一个 _form。 html.haml 文档位于插件中的 _form.html.erb 文档旁边,并且是相同的,显然,该插件不需要依赖于 Haml,但如果父应用程序正在渲染 Haml,它会看起来。不过,在寻找 ERB 视图之前,有一个需要注意的地方,那就是 Haml 实际上需要在应用程序的开头加载,所以我现在在 application.rb 的底部有一行内容。 'Haml' 本身就是

这样,对于其他遇到此问题的人,现在您有 3 种解决方案可供选择,如果您有更好的解决方案,请发布!

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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文