cheatsheets/slim.md

139 lines
2.0 KiB
Markdown
Raw Permalink Normal View History

2015-07-16 22:19:25 +02:00
---
title: Slim
2015-11-24 06:02:17 +01:00
category: Ruby libraries
2017-09-04 04:49:50 +02:00
prism_languages: [jade]
weight: -1
2015-07-16 22:19:25 +02:00
---
2017-09-04 04:49:50 +02:00
### Example
```jade
2015-07-16 22:19:25 +02:00
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')
```
2017-09-04 04:49:50 +02:00
### Attributes
```jade
meta[charset='utf-8']
meta(name="keywords" content="template language")
meta name="author" content=author
```
You can use parentheses, brackets, or none at all.
2015-07-16 22:19:25 +02:00
### Ruby attributes
2017-09-04 04:49:50 +02:00
```jade
2015-07-16 22:19:25 +02:00
a class=[:menu,:highlight]
2017-09-04 04:49:50 +02:00
```
You can use Ruby expressions in attributes.
### Hash attributes
```jade
2015-07-16 22:19:25 +02:00
.card *{'data-url' => place_path(place)}
```
2017-09-04 04:49:50 +02:00
You can destructure Ruby hashes as attributes.
2015-07-16 22:19:25 +02:00
2017-09-04 04:49:50 +02:00
### Inline Ruby
```jade
2015-07-16 22:19:25 +02:00
ruby:
def foobar
"hello"
end
div= foobar
```
### Inline Markdown
```jade
markdown:
### On Markdown
2018-03-06 08:38:54 +01:00
I am *Markdown* _text_!
{: .classname}
```
2018-03-06 08:38:54 +01:00
Slim can handle your [Markdown](https://daringfireball.net/projects/markdown/syntax) content for longer content blocks or `code`.
Depending on your parser, like [Kramdown](https://kramdown.gettalong.org/quickref.html), other features might work, like assigning attributes or classes.
2017-09-04 04:49:50 +02:00
### Embedded JavaScript
2015-07-16 22:19:25 +02:00
2017-09-04 04:49:50 +02:00
```jade
2015-07-16 22:19:25 +02:00
javascript:
alert('Slim supports embedded javascript!')
```
2015-07-16 22:19:25 +02:00
### Comments
2017-09-04 04:49:50 +02:00
```jade
2015-07-16 22:19:25 +02:00
/ Comment
/! HTML comment
```
### Ruby
2017-09-04 04:49:50 +02:00
```jade
2015-07-16 22:19:25 +02:00
== yield
= t('.hello')
- 3.times do |i|
div
```
### Verbatim text
2017-09-04 04:49:50 +02:00
```jade
2015-07-16 22:19:25 +02:00
div
| This is text
it is nice
```
2020-08-03 14:20:05 +02:00
### Advanced whitespaces
```jade
div
' This appends a whitespace
div
| This hackily prepends a whitespace
div
=> 'This appends a whitespace'
div
=< 'This prepends a whitespace'
```
2015-07-16 22:19:25 +02:00
### Inline HTML
2017-09-04 04:49:50 +02:00
```jade
2015-07-16 22:19:25 +02:00
<div class='foo'>
- if articles.empty?
| Nothing here
</div>
```
### Inline tags
2017-09-04 04:49:50 +02:00
```jade
2015-07-16 22:19:25 +02:00
ul
li: a(href='/') Home
```
2017-09-04 04:49:50 +02:00
### References
- <http://slim-lang.com/>