简体   繁体   中英

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
end

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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM