User:pgomata/sandbox
This is a user sandbox of Pgomata. A user sandbox is a subpage of the user's user page. It serves as a testing spot and page development space for the user and is not an encyclopedia article. |
Stable release | 3.0.6
/ September 15, 2010 |
---|---|
Operating system | Cross-platform |
Type | Template engine |
License | MIT |
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 what could be removed (<, >, closing tags, etc...) to 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"[2]. It's supported by major frameworks like Rails, Sinatra.
How does SLIM simplify the use of 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]SLIM started with a goal of reducing view syntax and achieve the similar behavior and performance.
A short list of the features[3]...
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:
- 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 engine like Markdown
Installation
[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.[4]
Usage
[edit]Here are a few examples of SLIM template usage[5]:
Syntax Examples
Verbatim Text
The pipe tells 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>
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.
Text Content
Either start on the same line as the tag.
html
body
h1 id="headline" Welcome to my site.
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
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
Short cuts
Tag shortcuts
html
Slim::Engine.set_options shortcut: {'c' => {tag: 'container'}, '#' => {attr: 'id'}, '.' => {attr: 'class'} }
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'}}
Tools
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
External Links
[edit]References
[edit]- ^ "Slim Github".
- ^ "Rubydoc". Rubydoc.
- ^ "Slim Lang".
- ^ "gems".
- ^ "rubydoc".