如何使值在所有 Liquid 模板中可用
我正在将 Liquid 与 Sinatra 结合使用,并希望在所有模板中都可以使用某个值(特别是 Sinatra::Application.environment
),而无需在每个 get/post 中将其定义为本地值。像这样:
在 app.rb (我的主应用程序文件)中:
# nothing in here about the variable
get '/some/route' do
# or here
liquid :my_template
end
在 app.rb - 我的主应用程序文件中,或者我可以要求/包含的内容:
some_awesome_technique do
def app_env
Sinatra::Application.environment
end
end
在任何模板中:
<p>
{% if environment == :development %}
Never see this in production
{% end %}
</p>
<!-- or even -->
<p>
{% if dev_mode %}
Or this...
{% endif %}
</p>
我并不真正关心实现,只要我不必在每条路线中放置冗余代码。提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样的事情就会
在你的模板中起作用:
Something like this will work
then in your template: