パーフェクトSlim(A Fast, Lightweight Template Engine for Ruby)

slim-lang.com

http://www.rubydoc.info/gems/slim/frames

Verbatim text |

The pipe tells Slim to just copy the line. It essentially escapes any processing. Each following line that is indented greater than the pipe is copied over.
※そのまま表示したいテキストの場合に|の下に書きます。

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

each

- @articles.each do |a|
  | <tr><td>#{a.name}</td><td>#{a.description}</td></tr>

※endは不要です。

  • テキストの間に半角スペースを置きたい場合
body
  p
    | This line is on the left margin.
       This line will have one space in front of it.
         This line will have two spaces in front of it.
           And so on...

Control code -

The dash denotes control code.

Examples of control code are loops and conditionals. end is forbidden behind -.

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

※改行したい場合は\を行末に置きますが、viewのロジックはdecoratorに置くべきです。

body
  - if articles.empty?
    | No inventory

  - hoge = @hoge.hoge

Output =

The equal sign tells Slim it's a Ruby call that produces output to add to the buffer.

If your ruby code needs to use multiple lines, append a backslash \ at the end of the lines

p = t("simple_form.hogehoge.hoge")

= link_to 'hoge', hoge_path

= javascript_include_tag \
   "jquery",
   "application"

※tはI18n.translateのaliasです。
※ちなみにjはescape_javascriptのaliasです

If your line ends with comma , (e.g because of a method call) you don't need the additional backslash before the linebreak. For trailing or leading whitespace the modifiers > and < are supported.

Output with trailing white space =>. Same as the single equal sign (=), except that it adds a trailing white space.
Output with leading white space =<. Same as the single equal sign (=), except that it adds a leading white space.

Output without HTML escaping ==

Same as the single equal sign (=), but does not go through the escape_html method.

For trailing or leading whitespace the modifiers > and < are supported.

Output without HTML escaping and trailing white space ==>. Same as the double equal sign (==), except that it adds a trailing white space.

Output without HTML escaping and leading white space ==<. Same as the double equal sign (==), except that it adds a leading white space.

Code comment /

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

※基本/でいいと思います。

IE conditional comment /[...]

/[if IE]
    p Get a better browser.

Doctype tag

doctype html

Attributes

You write attributes directly after the tag.

For normal text attributes you must use double " or single quotes ' (Quoted attributes).

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

Quoted attributes

a data-title="help" data-content="extremely long help text that goes on\
  and one and one and then starts over...."

Ruby attributes

Write the ruby code directly after the =. If the code contains spaces you have to wrap the code into parentheses (...).

You can also directly write hashes {...} and arrays [...].

body
  table
    - for user in users
      td id="user_#{user.id}" class=user.role
        a href=user_action(user, :edit) Edit #{user.name}
        a href=(path_to_user user) = user.name

ID shortcut # and class shortcut .

body
  h1#headline
    = page_headline
  h2#tagline.small.tagline
    = page_tagline
  .content
    = show_content

Text interpolation

To escape the interpolation (i.e. render as is)

body
  h1 Welcome \#{current_user.name} to the show.

Embedded engines (Markdown, ...)

coffee:
  square = (x) -> x * x

markdown:
  #Header
    Hello from #{"Markdown!"}
    Second Line!

p: markdown: Tag with **inline** markdown!