Erb 和 Sinatra 在 ruby 中
所以我有一个使用 sinatra 构建的网络服务器,其主要内容如下:
set :variable,"value"
get '/' do
erb :index
end
当然,views/index.erb
中的模板如下所示:
<html>
<!-- etc -->
<ul>
<% my_array.each do |thing| %>
<%="Something: #{thing}, variable from sinatra: #{settings.variable}"%>
<% end %>
</ul>
</html>
如果您尝试运行像这样的代码,您会注意到您无法从 erb 模板内部访问 sinatra 的设置变量。有什么想法可以在保持简单性的同时实现这一目标吗?
提前致谢!
So I have a webserver I've built using sinatra, the meat of which goes like this:
set :variable,"value"
get '/' do
erb :index
end
And, of course, the template in views/index.erb
which looks something like this:
<html>
<!-- etc -->
<ul>
<% my_array.each do |thing| %>
<%="Something: #{thing}, variable from sinatra: #{settings.variable}"%>
<% end %>
</ul>
</html>
If you try running code like this you'll notice that you can't access sinatra's settings variable from inside erb templates. Any ideas how I can achieve this while keeping its simplicity?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用的是旧版本的 Sinatra - 更新到版本 1.0 并且工作正常:)
谢谢大家!
I was using an old version of Sinatra - updated to version 1.0 and it works fine :)
Thanks everyone!
我遇到了类似的问题,解决方案是确保所有
set :x, "y"
内容都发生在类声明中(我的Sinatra::Base
子类) ) 在初始化方法之外。I had a similar issue and the resolution was to make sure all the
set :x, "y"
stuff happened in the class declaration (of my subclass ofSinatra::Base
) outside of the initialize method.