Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling the existence of an attribute

I have a problem with the Slim template engine in a Sinatra project. I have an edit form to be filled when the route is triggered. There is an issue with HTML select option. I need something like this when the edit form is loaded. Notice that Mrs. option is 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 about an attribute error. Then I tried something like this:

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

but then the output was something like this:

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

I guess the string"false" is interpreted as true. That failed. I tried some combinations with round brackets but couldn't get it to work.

How could I set the selected attribute of an option in a select list in Slim?

like image 703
Ziyan Junaideen Avatar asked Aug 27 '13 07:08

Ziyan Junaideen


People also ask

Which method is used to check if an attribute exists or not?

The hasAttribute() method returns true if the attribute exists, otherwise false .

How do you check if data attribute exists in Javascript?

Use the hasAttribute() method to check if a data attribute exists, e.g. if (el. hasAttribute('data-example')) {} . The hasAttribute method returns true if the provided attribute exists, otherwise false is returned.


1 Answers

For an attribute, you can write ruby code after the =, but if the ruby code has spaces in it, you have 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.

The brackets are optional, so you can also write it like this:

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

Or, instead of brackets, you can use a different 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 standalone 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 guess the string "false" is interpreted as true.

Yes. The only things that evaluate to false are false itself and nil. Any number(including 0), any string (including ""), and any array(including []), etc. are all true.

Not pertinent to your problem, but perhaps useful to some future searcher...I guess Slim looks up instance variables in whatever object you pass as an argument to render. So if you want to provide a whole bunch of values for the 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() even though they seem like natural candidates.

like image 162
7stud Avatar answered Oct 04 '22 07:10

7stud