How to make value available in all Liquid Templates

I use Liquid with Sinatra and want to make a specific value ( Sinatra::Application.environmentin particular) available in all templates, without defining it as local in every get / post. For instance:

In app.rb (my main application file):

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

In app.rb, my main application file or something that 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 really don't need an implementation unless I need to add redundant code to each route. Thanks in advance!

+3
source share
1 answer

Something like this will work

before do
  @env = Sinatra::Application.environment
end

then in your template:

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

All Articles