Jordan Running
Jordan Running

Reputation: 106027

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!

Upvotes: 3

Views: 585

Answers (1)

errorhandler
errorhandler

Reputation: 1767

Something like this will work

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

then in your template:

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

Upvotes: 3

Related Questions