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