控制属性的存在

Ziy*_*een 8 html ruby slim-lang

SlimSinatra项目中遇到模板引擎问题.我有一个编辑表单,当路由被触发时填写.有一个问题HTML select option.加载编辑表单时我需要这样的东西.请注意,该Mrs.选项是selected:

<select name="person[title]" id="person[title]">
  <option value="Mr.">Mr.</option>
  <option value="Mrs." selected>Mrs.</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我试过了:

option[value="Mrs." "#{person.title == :mrs ? 'selected' : ''}"]
Run Code Online (Sandbox Code Playgroud)

例外是关于属性错误.然后我尝试了这样的事情:

option[value="Mrs." selected="#{person.title == :mrs ? true : false}"]
Run Code Online (Sandbox Code Playgroud)

但后来输出是这样的:

<option value"Mrs." selected="false">Mrs.</option>
Run Code Online (Sandbox Code Playgroud)

我猜这个字符串"false"被解释为true.那失败了.我尝试了一些圆括号的组合,但无法让它工作.

我怎样才能在列表中设置a的selected属性?optionselectSlim

7st*_*tud 10

对于属性,您可以在之后编写ruby代码=,但是如果ruby代码中包含空格,则必须在ruby代码周围添加括号:

option[value="1" selected=("selected" if @title=="Mrs.")] "Mrs."
Run Code Online (Sandbox Code Playgroud)

请参阅此处的"Ruby属性":http://rdoc.info/gems/slim/frames.

括号是可选的,因此您也可以这样写:

option value="1" selected=("selected" if @title=="Mrs.") "Mrs."
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用不同的分隔符代替括号:

option {value="1" selected=("selected" if @title=="Mrs.")} "Mrs."
Run Code Online (Sandbox Code Playgroud)

这是一些代码:

slim.slim:

doctype html
html
  head
    title Slim Examples
    meta name="keywords" content="template language"

  body
    h1 Markup examples
    p This example shows you how a basic Slim file looks like.
    select
      option[value="1" selected=("selected" if @title=="Mr.")] "Mr."
      option[value="2" selected=("selected" if @title=="Mrs.")] "Mrs."
Run Code Online (Sandbox Code Playgroud)

在没有rails的独立ruby程序中使用Slim:

require 'slim'

template = Slim::Template.new(
  "slim.slim", 
  pretty: true  #pretty print the html
)

class Person
  attr_accessor :title

  def initialize title
    @title = title
  end
end

person = Person.new("Mrs.")
puts template.render(person)

--output:--
<!DOCTYPE html>
<html>
  <head>
    <title>
      Slim Examples
    </title>
    <meta content="template language" name="keywords" />
  </head>
  <body>
    <h1>
      Markup examples
    </h1>
    <p>
      This example shows you how a basic Slim file looks like.
    </p>
    <select><option value="1">"Mr."</option><option selected="selected" value="2">"Mrs."</option></select>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我猜字符串"false"被解释为true.

是.唯一评价为假的东西本身就是假的,而且是零.任何数字(包括0),任何字符串(包括"")和任何数组(包括[])等都是正确的.

与您的问题无关,但对未来的搜索者可能有用......我猜Slim会查找您传递的任何对象中的实例变量作为渲染参数.因此,如果您想为模板提供一大堆值,您可以编写:

require 'slim'

template = Slim::Template.new(
  "slim.slim", 
  pretty: true  #pretty print the html
)

class MyVals
  attr_accessor :count, :title, :animals

  def initialize count, title, animals
    @count = count
    @title = title
    @animals = animals
  end
end

vals = MyVals.new(4, "Sir James III", %w[ squirrel, monkey, cobra ])
puts template.render(vals)
Run Code Online (Sandbox Code Playgroud)

slim.slim:

doctype html
html
  head
    title Slim Examples
    meta name="keywords" content="template language"

  body
    p =@count
    p =@title
    p =@animals[-1]
Run Code Online (Sandbox Code Playgroud)

OpenStruct和Struct都不使用render(),即使它们看起来像是天生的候选者.