如果 CONDITION 为 true,我会尝试让 Slim 生成以下 HTML:
<div id="start_button" hidden="hidden">
我尝试过各种方法,比如显而易见的:
#start_button #{('hidden="hidden"' if CONDITION?)}
= link_to 'Get Started', ...etc...
但这会生成:
<div id="start_button">hidden="hidden"
= link_to 'Get Started',..etc...
我知道在将类等属性设置为某物时该怎么做,但因为它必须是“隐藏”的全部或全部,所以这给我带来了问题。
今晚我遇到了很多麻烦,所以非常感谢您的帮助!
最佳答案
文档中对此进行了说明:
Ruby attributes
Write the ruby code directly after the =. If the code contains spaces you have to wrap the code into parentheses (...). You can also directly write hashes {...} and arrays [...].
因此,()
内的任何内容都会被评估为 Ruby 代码,如果该语句被评估为 false,则它会以某种方式“跳过”,否则它会执行 true 分支中所述的操作。
#start_button hidden=('hidden' if true)
<div hidden="hidden" id="start_button"></div>
#start_button hidden=('hidden' if false)
<div id="start_button"></div>
关于ruby-on-rails - 使用 Ruby on Rails 有条件地在 Slim 中创建隐藏属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67492893/