2012-01-21 30 views
1

有没有办法在Rails中禁用ERB处理程序?在Rails中禁用ERB处理程序

这可能听起来很愚蠢,但我们正在迁移到SLIM,并希望 防止一些懒惰的开发人员仍然使用ERB。

+0

我会先在ActionView :: Template :: Handlers中戳一下;这是注册erb处理程序的原因。最简单的方法可能是对注销人员进行猴子补丁并取消注册。 –

回答

5

据我所见,有没有办法unregister模板处理程序。

但是我们可以通过黑客来实现。

ActionView::Template::Handlers::ERB有以下几行;

self.class.erb_implementation.new(
    erb, 
    :trim => (self.class.erb_trim_mode == "-") 
).src 

因此,让我们打破它,为乐趣。

我们将添加一个初始config/initializers/no_erb_allowed.rb

class NoErbAllowed 
    def initialize(*args) 
    raise "ERB is not allowed" 
    end 
end 

ActionView::Template::Handlers::ERB.erb_implementation = NoErbAllowed 

任何试图使用,然后再培训局会引发错误

ActionView::Template::Error (ERB is not allowed): 
+0

+1,更简单; -0.25,丑陋。 –

+0

他们在使用ERB – Uchenna

+1

@UchennaOkafor时有什么不对吗,包括我在内的一些人更喜欢避免erb的冗长。 – Zabba

1

我认为这应该工作的任何观点:

handlers = ActionView::Template::Handlers.class_variable_get :@@template_handlers 

handlers.delete :erb 

ActionView::Template::Handlers.class_variable_set :@@template_handlers, handlers 

基本上这得到@@template_handlers散列从ActionView::Template::Handlers,删除:erb键(指向ERB处理程序)并将其写回该类。

这可能会在initializer。它需要ActionView::Template::Handlers(显然)之后加载,但自己加载的处理程序之前,所以我觉得属于在to_preparebefore_eager_load初始化,例如:

module YourApp 
    class Application < Rails::Application 
    config.before_eager_load do 
     handlers = ActionView::Template::Handlers.class_variable_get :@@template_handlers 

     handlers.delete :erb 

     ActionView::Template::Handlers.class_variable_set :@@template_handlers, handlers 

    end 

    end 
end 

希望这是有帮助!

相关问题