Attribute Management

I have a problem with Slim template in Sinatra project. I have an editing form that will be filled in when the route starts. There is a problem with the HTML select option . I need something similar when loading an edit form. Please note that the option is Mrs. selected :

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

I tried:

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

The exception was due to an attribute error. Then I tried something like this:

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

but then the result was something like this:

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

I assume that the string "false" interpreted as true . This failed. I tried some combinations with parentheses but couldn't make it work.

How can I set the selected attribute for option in the select list in Slim ?

+6
source share
1 answer

For an attribute, you can write a ruby ​​code after = , but if there are spaces in the ruby ​​code, you need to put parentheses around the ruby ​​code:

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

See "Ruby Attributes" here: http://rdoc.info/gems/slim/frames .

Brackets are optional, so you can also write them like this:

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

Or instead of parentheses, you can use another delimiter:

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

Here it is with some code:

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

Using Slim in a separate ruby ​​program without rails:

 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> 

I assume the string "false" is interpreted as true.

Yes. The only thing that evaluates to false is false and nil. Any number (including 0), any string (including "") and any array (including []), etc. - that's right.

Not appropriate for your problem, but perhaps useful for a future crawler ... I think Slim is looking for instance variables in any object that you pass as an argument for rendering. Therefore, if you want to provide a whole bunch of values ​​for a template, you can write:

 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) 

slim.slim:

 doctype html html head title Slim Examples meta name="keywords" content="template language" body p =@count p =@title p =@animals [-1] 

Neither OpenStruct nor Struct work with render (), although they seem like natural candidates.

+8
source

Source: https://habr.com/ru/post/952564/


All Articles