将 HAML 添加到 Rails 资产管道
我想提供已通过 HAML 预处理的客户端模板。我尝试使用 haml_assets gem 并将以下代码添加到初始化程序中:
Rails.application.assets.register_engine ".haml", Tilt::HamlTemplate
这两种方法都提供原始数据当我访问资产时,HAML 且未编译 HAML。如何将 HAML 添加到管道中?
I would like to serve client side templates that have been pre-processed through HAML. I have tried using the haml_assets gem and adding the following code to an initializer:
Rails.application.assets.register_engine ".haml", Tilt::HamlTemplate
Both of these methods serve the raw HAML and not compiled HAML when I access the asset. How can I add HAML to the pipeline?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只是为了澄清事情,因为我发现当前的答案有点令人恼火(尽管引导我走向了正确的方向),
如果我在初始化程序文件中包含此行,它就可以工作:
将您的 haml 文件放入资产文件夹中,例如
:不过不使用haml_asset gem!
Just to clear things up, since I find the current answers a bit irritating (led me to the right direction though)
It works, if i have this line in an initializer file:
Throw your haml files into the assets folder, for example:
Do not use the haml_asset gem though!
application.rb 中的以下代码在 Rails 3.2 中适用于我(无论是在开发中还是在预编译后的生产中):
这允许您将模板放在以后缀 .html.haml 命名的 app/assets/templates 中(您需要其中的 .html 或在预编译过程中生成 .htm 文件而不是 .html)。
The following code in application.rb works for me in Rails 3.2 (both in development and in production after pre-compilation):
This allows you to put you templates in app/assets/templates named with the suffix .html.haml (you need the .html in there or else .htm files get generated instead of .html in the pre-compilation process).
这对我有用:
这适用于 http://127.0.0.1:3000/assets/test .html.haml
Rails.application.assets
是一个Sprockets::Environment
。请参阅此处的 API 参考:
This works for me:
This works for http://127.0.0.1:3000/assets/test.html.haml
Rails.application.assets
is aSprockets::Environment
.See here for API reference:
使用与我相同的方法:
作为纯 haml 返回。但
被用作 html 块。所以我认为这与哈姆宝石有关。您可以通过以下方式强制进行 haml 转换:
希望它有帮助......
Using the same approach I've got:
returned as pure haml. But
was served as html chunk. So I think this is something with haml gem. You can force haml conversion with something like this:
Hope it helps...
在我们得到完整的解决方案之前,必须将之前的两个答案合并起来。
以下行在开发中有效:
但随后在任何预编译中都会失败。
为了让资产服务的 haml 在预编译后工作,我们还需要 application.rb 中的这些行:
Two of the previous answers here had to be combined before we had a full solution.
The following line works in development:
But then fails on any precompile.
For asset-served haml to work after a precompile, we also needed these lines in application.rb:
带链轮 3 和 4
With sprockets 3 and 4