Erb 和 Sinatra 在 ruby​​ 中

发布于 2024-08-27 08:22:51 字数 499 浏览 5 评论 0原文

所以我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

硬不硬你别怂 2024-09-03 08:22:51

我使用的是旧版本的 Sinatra - 更新到版本 1.0 并且工作正常:)

谢谢大家!

I was using an old version of Sinatra - updated to version 1.0 and it works fine :)

Thanks everyone!

醉态萌生 2024-09-03 08:22:51

我遇到了类似的问题,解决方案是确保所有 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 of Sinatra::Base) outside of the initialize method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文