【发布时间】:2014-03-15 23:58:33
【问题描述】:
我正在尝试将 (if ... else) 的结果传递给 .erb 文件 例如
if @p
"show something"
else
"show other thing"
end
我该怎么做? 谢谢
【问题讨论】:
-
是用haml写的,但是我用的是.erb,试过了,还是不行。谢谢
-
对不起..我没有检查
我正在尝试将 (if ... else) 的结果传递给 .erb 文件 例如
if @p
"show something"
else
"show other thing"
end
我该怎么做? 谢谢
【问题讨论】:
在 Sinatra 中向视图传递值的常用方法是使用实例变量。您在路由中设置的任何实例变量都将在视图中可用。所以在这种情况下,你可以这样做:
@my_variable = if @p
"show something"
else
"show other thing"
end
然后在您的 Erb 视图中:
<%= @my_variable %>
结果将是show something 或show other thing 将根据@p 的值出现在呈现的页面中。
【讨论】: