我正在将 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>
只要我不必在每条路线中放置冗余代码,我就不会真正关心实现。提前致谢!