2012-01-21 70 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 

希望這是有幫助!

相關問題