SLIM
Stable release
3.0.6 / September 15, 2010 (2010-09-15)
Operating systemCross-platform
TypeTemplate engine
LicenseMIT

SLIM is a template language/engine[1]. The aim of this is to reduce the view syntax to just the needed part without being cryptic. The base was taken as HTML template and how much could be removed (<, >, closing tags, etc...) and make the template more flexible in nature. Slim was developed right from the start with performance in mind. Slim's core syntax is guided by one thought: "What's the minimum required to make this work". Its supported by major frameworks like Rails, Sinatra.

How does SLIM incorporate HTML?

edit

SLIM works on the simple understandable free flow coding. So it removes the numerous tags in HTML and replaces them with indentation.

<html>
  <body>
     <h1>
        The content of the document......
     </h1>
  </body>
</html>

In SLIM

html
   body
      h1 The content of the document......

Indentation matters, but the indentation depth can be chosen as you like. If you want to first indent 2 spaces, then 5 spaces.


Features

edit

The slim started with a goal of reducing view syntax and achieve the similar behavior and performance. The flexibility of slim increased proportionally with huge number of people taking interest and contributing to slim.

A short list of the features[2]...

Elegant syntax

  • HTML style mode with closing tags
  • Configurable shortcut tags

Safety

  • Automatic HTML escaping by default
  • Support for Rails' html_safe?

Highly configurable

Extensible via the following plugins:

  • Logic less mode similar to Mustache
  • High performance
  • Comparable speed to ERB/Erubis
  • Streaming support in Rails
  • Supported by all major frameworks (Rails, Sinatra, ...)

Full Unicode support for tags and attributes

Embedded engines like Markdown and Textile

Install

edit

In the Rails environment slim can be installed as a Gem


One line Command to install gem

 gem install slim

Now just use the .slim extension when u save the template. Another thing to note is SLIM has some run time dependencies namely temple and tilt.[3]

Usage

edit

Here are a few examples of slim template usage[4]:

Syntax Examples

Verbatim Text

The pipe tells the slim to just copy the line following pipe.

body
  p
    |
      This is a test of the text block.

Inline html

Slim allows you to write both html coding and a mix of html and slim style.

html
  head
    title Example
  <body>
    - if articles.empty?
    - else
      table
        - articles.each do |a|
          <tr><td>#{a.name}</td><td>#{a.description}</td></tr>
  </body>
 html


Control Code

The dash statement at the beginning denotes a control code Examples of control code are loops and conditionals. Blocks are defined only by indentation. If your ruby code needs to use multiple lines, append a backslash \ at the end of the lines. If your line ends with comma , (e.g because of a method call) you don't need the additional backslash before the line break.

html
  body
  - if articles.empty?
    | No inventory
 html


Code Comment

Use the forward slash for code comments - anything after it won't get displayed in the final render. Use / for code comments and /! for html comments.

 html
  body
  p
    / This line won't get displayed.
      Neither does this line.
    /! This will get displayed as html comments in the html file.
 html

Text Content

Either start on the same line as the tag.

html
body
  h1 id="headline" Welcome to my site.
html

Or nest it. You must use a pipe or an apostrophe to escape processing

html
body
  h1 id="headline"
    | Welcome to my site.</html>


Dynamic Content

Can make the call on the same line

html
body
  h1 id="headline" = page_headline
html

Or nest it.

html
body
  h1 id="headline"
    = page_headline
html


Attributes

Attributes can be directly written after tags.

html
a href="http://slim-lang.com" title='Slim Homepage' Goto the Slim homepage
html


Short cuts

Tag shortcuts

html
Slim::Engine.set_options shortcut: {'c' => {tag: 'container'}, '#' => {attr: 'id'}, '.' => {attr: 'class'} }
html

Attribute Shortcuts

You can define custom tag shortcuts by setting the option :shortcut.

html
Slim::Engine.set_options shortcut: {'&' => {tag: 'input', attr: 'type'}, '#' => {attr: 'id'}, '.' => {attr: 'class'}}
html


Tools

The gem 'slim' comes with the small tool 'slimrb' to test Slim from the command line.

<html>
$ slimrb --help
Usage: slimrb [options]
    -s, --stdin                      Read input from standard input instead of an input file
        --trace                      Show a full traceback on error
    -c, --compile                    Compile only but do not run
    -e, --erb                        Convert to ERB
        --rails                      Generate rails compatible code (Implies --compile)
    -r, --require library            Load library or plugin with -r slim/plugin
    -p, --pretty                     Produce pretty html
    -o, --option name=code           Set slim option
    -l, --locals Hash|YAML|JSON      Set local variables
    -h, --help                       Show this message
    -v, --version                    Print version
</html>

References

edit
  1. ^ "Slim Github".
  2. ^ "Slim Lang".
  3. ^ "gems".
  4. ^ "rubydoc".