2011-03-13 102 views
3

我在Liquid中使用了Sinatra,並且想要在所有模板中使用特定值(Sinatra::Application.environment),而無需在每個get/post中將其定義爲本地值。像這樣:如何在所有液體模板中創建一個值

在app.rb(我主要的應用程序文件):

# nothing in here about the variable 
get '/some/route' do 
    # or here 
    liquid :my_template 
end 

在app.rb - 我的主應用程序文件,或者是我可以要求/包括:

some_awesome_technique do 
    def app_env 
    Sinatra::Application.environment 
    end 
end 

在任何模板:

<p> 
    {% if environment == :development %} 
    Never see this in production 
    {% end %} 
</p> 

<!-- or even --> 

<p> 
    {% if dev_mode %} 
    Or this... 
    {% endif %} 
</p> 

我真的不關心執行,只要我沒有把多餘的代碼每條路線。提前致謝!

回答

3

像這樣的事情會工作

before do 
    @env = Sinatra::Application.environment 
end 
在你的模板

則:

{% if @env == :development %} 
    Boo! 
{% endif %}