将 HAML 添加到 Rails 资产管道

发布于 2024-12-09 15:00:43 字数 292 浏览 1 评论 0原文

我想提供已通过 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 技术交流群。

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

发布评论

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

评论(6

神爱温柔 2024-12-16 15:00:43

只是为了澄清事情,因为我发现当前的答案有点令人恼火(尽管引导我走向了正确的方向),

如果我在初始化程序文件中包含此行,它就可以工作:

# config/initializers/haml_assets.rb
Rails.application.assets.register_engine '.haml', Tilt::HamlTemplate

将您的 haml 文件放入资产文件夹中,例如

# app/assets/templates   

:不过使用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:

# config/initializers/haml_assets.rb
Rails.application.assets.register_engine '.haml', Tilt::HamlTemplate

Throw your haml files into the assets folder, for example:

# app/assets/templates   

Do not use the haml_asset gem though!

雨巷深深 2024-12-16 15:00:43

application.rb 中的以下代码在 Rails 3.2 中适用于我(无论是在开发中还是在预编译后的生产中):

require 'haml'

config.assets.paths << Rails.root.join("app", "assets", "templates")

class HamlTemplate < Tilt::HamlTemplate
  def prepare
    @options = @options.merge :format => :html5
    super
  end
end

config.before_initialize do |app|
  require 'sprockets'
  Sprockets::Engines #force autoloading
  Sprockets.register_engine '.haml', HamlTemplate
end

这允许您将模板放在以后缀 .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):

require 'haml'

config.assets.paths << Rails.root.join("app", "assets", "templates")

class HamlTemplate < Tilt::HamlTemplate
  def prepare
    @options = @options.merge :format => :html5
    super
  end
end

config.before_initialize do |app|
  require 'sprockets'
  Sprockets::Engines #force autoloading
  Sprockets.register_engine '.haml', HamlTemplate
end

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).

疏忽 2024-12-16 15:00:43

这对我有用:

# app/assets/javascripts/test.html.haml

%p hello

# config/initializers/haml_template.rb

Rails.application.assets.register_mime_type 'text/html', '.html'
Rails.application.assets.register_engine '.haml', Tilt::HamlTemplate

这适用于 http://127.0.0.1:3000/assets/test .html.haml

Rails.application.assets 是一个 Sprockets::Environment

请参阅此处的 API 参考:

This works for me:

# app/assets/javascripts/test.html.haml

%p hello

# config/initializers/haml_template.rb

Rails.application.assets.register_mime_type 'text/html', '.html'
Rails.application.assets.register_engine '.haml', Tilt::HamlTemplate

This works for http://127.0.0.1:3000/assets/test.html.haml

Rails.application.assets is a Sprockets::Environment.

See here for API reference:

明天过后 2024-12-16 15:00:43

使用与我相同的方法:

%tr
  %th
    %a.action.link.show
  %td
  %td

作为纯 haml 返回。但

%tr
  %th
    %a.action.link.show
  %td cell 2
  %td cell 3

被用作 html 块。所以我认为这与哈姆宝石有关。您可以通过以下方式强制进行 haml 转换:

%tr
  %th
    %a.action.link.show
  %td  
  %td  

希望它有帮助......

Using the same approach I've got:

%tr
  %th
    %a.action.link.show
  %td
  %td

returned as pure haml. But

%tr
  %th
    %a.action.link.show
  %td cell 2
  %td cell 3

was served as html chunk. So I think this is something with haml gem. You can force haml conversion with something like this:

%tr
  %th
    %a.action.link.show
  %td  
  %td  

Hope it helps...

﹂绝世的画 2024-12-16 15:00:43

在我们得到完整的解决方案之前,必须将之前的两个答案合并起来。

以下行在开发中有效:

# config/initializers/haml_assets.rb
Rails.application.assets.register_engine '.haml', Tilt::HamlTemplate

但随后在任何预编译中都会失败。

为了让资产服务的 haml 在预编译后工作,我们还需要 application.rb 中的这些行:

require 'haml'

config.assets.paths << Rails.root.join("app", "assets", "templates")

class HamlTemplate < Tilt::HamlTemplate
  def prepare
    @options = @options.merge :format => :html5
    super
  end
end

config.before_initialize do |app|
  require 'sprockets'
  Sprockets::Engines #force autoloading
  Sprockets.register_engine '.haml', HamlTemplate
end

Two of the previous answers here had to be combined before we had a full solution.

The following line works in development:

# config/initializers/haml_assets.rb
Rails.application.assets.register_engine '.haml', Tilt::HamlTemplate

But then fails on any precompile.

For asset-served haml to work after a precompile, we also needed these lines in application.rb:

require 'haml'

config.assets.paths << Rails.root.join("app", "assets", "templates")

class HamlTemplate < Tilt::HamlTemplate
  def prepare
    @options = @options.merge :format => :html5
    super
  end
end

config.before_initialize do |app|
  require 'sprockets'
  Sprockets::Engines #force autoloading
  Sprockets.register_engine '.haml', HamlTemplate
end
攒一口袋星星 2024-12-16 15:00:43

带链轮 3 和 4

# in /config/initializers/haml.rb
Rails.application.config.assets.configure do |env|
 env.register_mime_type "text/haml", extensions: %w(.haml .html.haml)
 env.register_transformer 'text/haml', Tilt::HamlTemplate.default_mime_type, Tilt::HamlTemplate
end

With sprockets 3 and 4

# in /config/initializers/haml.rb
Rails.application.config.assets.configure do |env|
 env.register_mime_type "text/haml", extensions: %w(.haml .html.haml)
 env.register_transformer 'text/haml', Tilt::HamlTemplate.default_mime_type, Tilt::HamlTemplate
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文