2013-08-27 7 views
6

SinatraプロジェクトのSlimテンプレートエンジンに問題があります。ルートがトリガーされたときに入力する編集フォームがあります。 HTML select optionに問題があります。編集フォームが読み込まれたら、このようなものが必要です。 Mrs.オプションがselectedであることに注意してください:属性の存在の制御

<select name="person[title]" id="person[title]"> 
    <option value="Mr.">Mr.</option> 
    <option value="Mrs." selected>Mrs.</option> 
</select> 

は、私が試した:

option[value="Mrs." "#{person.title == :mrs ? 'selected' : ''}"] 

例外は、属性エラーについてでした。それから私はこのような何か試してみました:

option[value="Mrs." selected="#{person.title == :mrs ? true : false}"] 

をが、その後、出力は次のようなものだった:私は、文字列"false"trueとして解釈される推測

<option value"Mrs." selected="false">Mrs.</option> 

。それは失敗した。私は丸い括弧でいくつかの組み合わせを試みたが、それを動作させることができませんでした。

の属性は、のselectのリストにSlimと設定できますか?属性については

答えて

8

、あなたは=後にRubyコードを書くことができますが、Rubyコードにスペースが含まれる場合は、Rubyコードの前後に括弧を挿入する必要があります。

option[value="1" selected=("selected" if @title=="Mrs.")] "Mrs." 

は、ここでは、「Rubyの属性」を参照してください。 :http://rdoc.info/gems/slim/frames

あなたも、このようにそれを書くことができるようにブラケットは、オプションです。

option value="1" selected=("selected" if @title=="Mrs.") "Mrs." 

または、代わりに括弧で、あなたは別の区切り文字を使用することができます。

option {value="1" selected=("selected" if @title=="Mrs.")} "Mrs." 

ここではいくつかでありますコード:

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." 

レールせずにスタンドアロンRubyプログラムでスリムを使用:

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> 

私は「偽」の文字列がtrueと解釈されると思います。

はい。偽に評価される唯一のものは、偽自体と無しです。任意の数(0を含む)、任意の文字列( ""を含む)、および任意の配列([]を含む)などがすべてtrueです。

あなたの問題には関係ありませんが、将来の調査者にとってはおそらく役に立ちます... 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) 

スリム。スリム:

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

    body 
    p [email protected] 
    p [email protected] 
    p [email protected][-1] 

OpenStructもStructも、自然な候補のように見えますが、render()で動作しません。

関連する問題