gpt4 book ai didi

ruby-on-rails-3 - 我可以在 Coffeebeans/js.coffee View 中使用 HAML 风格的嵌入式 ruby​​ 吗?

转载 作者:行者123 更新时间:2023-12-03 17:56:08 26 4
gpt4 key购买 nike

我的 rails Controller 经常返回 JS,我使用 Coffeebeans 来允许我拥有 js.coffee View 。唯一的问题是它使用了 <%= ... %>嵌入式 Ruby 的语法...我希望使用 Coffeescript/HAML 风格的字符串插值,即:#{..} , 和 HAML 样式标签,即代替 <%= ... %> ,只需使用 =带有适当的缩进。

我想这种语法最好与文件扩展名 js.coffee.haml 一起使用。这可能吗?简单地使用该扩展名保存我的文件是行不通的,我猜需要对 Coffeebeans 进行一些调整以允许这样做,但我不知道我需要做什么。

这篇文章表明这​​是可能的:Chaining template handlers in Rails 3

关于如何解决这个问题的任何建议?

最佳答案

您链接到的问题有第二个答案,该答案链接到了posterous 上的一篇博客文章,现在已经死了……但是I found it on archive.org!该帖子还包含 a gist with the code在初始化程序中需要,以便您可以命名以 js.coffee_haml 结尾的文件并让他们处理。

为了防止将来出现死链接,这里是要点中的代码,但我没有编写它,也没有测试过它是否仍然有效:

module Coffee
module Rails
class HamlTemplateHandler
def self.haml_handler
@@haml_handler ||= ActionView::Template.registered_template_handler(:haml)
end

def self.call(template)
compiled_source = haml_handler.call(template)
"CoffeeScript.compile(begin;#{compiled_source};end)"
end
end
end
end

ActiveSupport.on_load(:action_view) do
ActionView::Template.register_template_handler :coffee_haml, Coffee::Rails::HamlTemplateHandler
end

博客文章中还提到 haml 不允许嵌套纯文本,因此如果您想在 coffeescript 中执行以下操作:
if xyz == 1
do_this y
do_that x

你必须把它包裹在 :plain筛选:
:plain
if xyz == 1
do_this y
do_that x

关于ruby-on-rails-3 - 我可以在 Coffeebeans/js.coffee View 中使用 HAML 风格的嵌入式 ruby​​ 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11232885/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com