开发者

How to make a value available in all Liquid templates

开发者 https://www.devze.com 2023-02-16 17:15 出处:网络
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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号