How to make a value available in all Liquid templates
I'm using Liquid with Sinatra and would like to make a certain value (Sinatra::Application.environment
, specifically) available in all templates without defining it as a local in every get/post. Like so:
In app.rb (my main application file):
# nothing in here about the variable
get '/some/route' do
# or here
liquid :my_template
en开发者_如何学Cd
In app.rb--my main application file, or something I can require/include:
some_awesome_technique do
def app_env
Sinatra::Application.environment
end
end
In any template:
<p>
{% if environment == :development %}
Never see this in production
{% end %}
</p>
<!-- or even -->
<p>
{% if dev_mode %}
Or this...
{% endif %}
</p>
I don't really care about the implementation as long as I don't have to put redundant code in every route. Thanks in advance!
Something like this will work
before do
@env = Sinatra::Application.environment
end
then in your template:
{% if @env == :development %}
Boo!
{% endif %}
精彩评论