Logic less mode is inspired by Mustache. Logic less mode uses a dictionary object e.g. a recursive hash tree which contains the dynamic content.
If the object is not false or empty?, the content will show
- article
h1 = title
If the object is false or empty?, the content will show
-! article
p Sorry, article not found
If the object is an array, the section will iterate
- articles
tr: td = title
Like mustache, Slim supports lambdas.
= person
= name
The lambda method could be defined like this
def lambda_method
"<div class='person'>#{yield(name: 'Andrew')}</div>"
end
You can optionally pass one or more hashes to yield
. If you pass multiple hashes, the block will be iterated as described above.
Example code:
- article
h1 = title
The dictionary object is accessed in the order given by the :dictionary_access
. Default order:
:symbol
- Ifarticle.respond_to?(:has_key?)
andarticle.has_key?(:title)
, Slim will executearticle[:title]
:string
- Ifarticle.respond_to?(:has_key?)
andarticle.has_key?('title')
, Slim will executearticle['title']
:method
- Ifarticle.respond_to?(:title)
, Slim will executearticle.send(:title)
:instance_variable
- Ifarticle.instance_variable_defined?(@title)
, Slim will executearticle.instance_variable_get @title
If all the above fails, Slim will try to resolve the title reference in the same order against the parent object. In this example, the parent would be the dictionary object you are rendering the template against.
As you might have guessed, the article reference goes through the same steps against the dictionary. Instance variables are not allowed in the view code, but Slim will find and use them. Essentially, you're just dropping the @ prefix in your template. Parameterized method calls are not allowed.
The self
keyword will return the .to_s
value for the element under consideration.
Given
{
article: [
'Article 1',
'Article 2'
]
}
And
- article
tr: td = self
This will yield
<tr>
<td>Article 1</td>
</tr>
<tr>
<td>Article 2</td>
</tr>
Install:
$ gem install slim
Require:
gem 'slim', require: 'slim/logic_less'
You might want to activate logic less mode only for a few actions, you should disable logic-less mode globally at first in the configuration
Slim::Engine.set_options logic_less: false
and activate logic less mode per render call in your action
class Controller
def action
Slim::Engine.with_options(logic_less: true) do
render
end
end
end
Sinatra has built-in support for Slim. All you have to do is require the logic less Slim plugin. This can be done in your config.ru:
require 'slim/logic_less'
You are then ready to rock!
You might want to activate logic less mode only for a few actions, you should disable logic-less mode globally at first in the configuration
Slim::Engine.set_options logic_less: false
and activate logic less mode per render call in your application
get '/page'
slim :page, logic_less: true
end
Type | Name | Default | Purpose |
---|---|---|---|
Boolean | :logic_less | true | Enable logic less mode (Enabled if 'slim/logic_less' is required) |
String | :dictionary | "self" | Dictionary where variables are looked up |
Symbol/Array<Symbol> | :dictionary_access | [:symbol, :string, :method, :instance_variable] | Dictionary access order (:symbol, :string, :method, :instance_variable) |