So, what is logic less mode and how do you use it? Examples are best, lets start with the basics, conditionals:
/ If the method is not false or empty?, the content will show - article h1 = title
Inverted conditional
/ If the method is false or empty?, the content will show -! article p Sorry, article not found
Next we'll go over resolution order.
If article.respond_to?(:title)
- article / Slim will execute article.send(:title) h1 = title
If article.respond_to?(:has_key?) and article.has_key?(:title)
- article / Slim will execute article[:title] h1 = title
If article.instance_variable_defined?(@title)
- article / Slim will execute article.instance_variable_get @title h1 = 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 initial scope you are rendering the template against.
As you might have guessed, the article reference goes through the same steps against the scope.
Things to know
Instance variables are not allowed in the view code, but Slim will find and use them. Essentially, you're just using dropping the @ prefix in your template.
Parametized method calls are not allowed.
Logic less support in Rails
Install:
$ gem install slim
Require:
gem 'slim', :require => 'slim/rails'
To enable logic less mode, put this line in config/environment.rb:
Slim::Engine.set_default_options :sections => true
Logic less support in Sinatra
Sinata will have built-in support for Slim in their next release, but until then you'll need this little snippet of code:
module Sinatra module Templates def slim(template, options={}, locals={}) render :slim, template, options, locals end end end
Now that's out of the way, all you have to do is require slim and set the option. This can be done in your config.ru:
require 'slim' Slim::Engine.set_default_options :sections => true
You are then ready to rock!
Source: slim-template/slim | Slim maintained by minad | Logo & design by activestylus
Copyrights | 2024 | slim-lang.com