Cheat Sheet速查表
跳转到主要内容

Slim 速查表

本文根据 Devhints / Slim 翻译。代码示例及版本范围沿用原文。

示例

doctype html
html
  head
    title Slim Examples
    meta[charset='utf-8']
    meta(name="keywords" content="template language")
    meta name="author" content=author
    meta property='og:image' content=asset_url('foo.png')
    meta property='og:image' content=(path_to_user user)
    meta(
      property='description'
      content='this is the song that never ends')

属性

meta[charset='utf-8']
meta(name="keywords" content="template language")
meta name="author" content=author

可以使用圆括号、方括号,或者什么都不用。

Ruby 属性

a class=[:menu,:highlight]

可以在属性中使用 Ruby 表达式。

哈希 (Hash) 属性

.card *{'data-url' => place_path(place)}

可以将 Ruby 哈希解构为属性。

内联 Ruby

ruby:
  def foobar
    "hello"
  end

div= foobar

内联 Markdown

markdown:
  ### On Markdown

  I am *Markdown* _text_!
  {: .classname}

Slim 可以处理您的 Markdown 内容(针对较长的内容区块),或者 code。根据您所使用的解析器(例如 Kramdown),可能还支持其他功能,比如指定属性或类 (class)。

嵌入式 JavaScript

javascript:
  alert('Slim supports embedded javascript!')

注释

/ Comment
/! HTML comment

Ruby

== yield
= t('.hello')
- 3.times do |i|
  div

原样文本 (Verbatim text)

div
  | This is text
    it is nice

高级空白处理

div
  ' This appends a whitespace
div
  |  This hackily prepends a whitespace
div
  => 'This appends a whitespace'
div
  =< 'This prepends a whitespace'

内联 HTML

<div class='foo'>
  - if articles.empty?
    | Nothing here
</div>

内联标签

ul
  li: a(href='/') Home

参考资料