SlideShare a Scribd company logo
1 of 22
Download to read offline
The Skinny On Slim
Presentation by Eric Mulligan
➢ What Is Slim?
➢ Why Slim?
➢ Syntax
➢ Features
➢ Slim vs HAML & ERB
➢ Demo
Outline
➢ A template engine for Ruby.
➢ Developed by Daniel Mendler in 2010.
➢ Similar to HAML & Jade (JavaScript).
➢ Supports Rails 3 & 4, Sinatra and other Rack-based
web frameworks.
➢ Tested on all major Ruby implementations.
What Is Slim?
➢ Core syntax is guided by one thought:
“What is the minimum required to make this work?”
➢ Allows us to write minimal templates.
➢ Syntax is aesthetically pleasing which makes it fun to
write templates.
➢ Designed with performance in mind.
➢ Guarantees well-formed HTML and XML.
➢ Faster to compile than HAML.
Why Slim?
Installation:
From the command line:
From a Gemfile:
Verbatim Text:
‘|’ tells Slim to copy the line and does no
processing. You can even embed HTML and it
will simply output it as is.
Syntax
gem install slim
gem ‘slim’
| This is some random text.
Outputs:
This is some random text.
| <p>A paragraph.</p>
Outputs:
<p>A paragraph</p>
Control Code:
The ‘-’ denotes control code such as
conditionals and loops. ‘end’ is forbidden as
blocks are defined by indentation only.
Dynamic Content:
The ‘=’ outputs the result of the Ruby call and
adds the output to the buffer.
The ‘==’ is the same as ‘=’, however, all HTML
is not escaped which is perfect for ‘yield’ and
‘render’ method calls.
- if @users.empty?
- @users.each do |user|
| I am a user.
= javascript_include_tag ‘application’
p = link_to ‘Link’, ‘#’
== yield
== render ‘partials/form’, locals: { user: @user }
Syntax (cont.)
Comments:
‘/’ does not output any markup.
‘/!’, however, outputs HTML comments.
‘/[if IE]’ outputs IE conditional comments.
Syntax (cont.)
/ This line won’t be displayed.
/! This line will be displayed as an HTML comment.
Outputs:
<!-- This line will be displayed as an HTML comment →
/[if IE]
p Some paragraph.
Outputs:
<--![if IE]--><p>Some paragraph</p><![endif]-->
DOCTYPE:
HTML doctypes:
XML doctypes:
doctype html
doctype 5
Outputs:
<!DOCTYPE html>
doctype 1.1
Outputs:
<!DOCTYPE html PUBLIC - “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
doctype xml
Outputs:
<?xml version=”1.0” encoding=”UTF-8” ?>
Syntax (cont.)
Inline Tags:
Adding ‘:’ between tags allows us to add
multiple tags in one single line.
Attributes:
Wrapping attributes is not required, but it does
improve readability. We can wrap attributes
with ‘{}’, ‘()’ and ‘[]’.
ul.alert.alert-danger: li: a href=”/” First link
Outputs:
<ul class=”alert alert-danger”>
<li><a href=”/”>First link</a></li>
</ul>
a href=”/” rel=”nofollow” Link Text
a[href=”/” rel=”nofollow”] Link Text
a(href=”/” rel=”nofollow”) Link Text
a{href=”/” rel=”nofollow”} Link Text
Outputs:
<a href=”/” rel=”nofollow”>Link Text</a>
Syntax (cont.)
Attributes (cont.):
Slim permits us to wrap attributes with ‘*{}’
which allows us to use a Ruby Hash-style
syntax.
Slim allows attribute values to be Ruby code
that outputs a string.
a*{:href => ‘/’, rel: ‘nofollow’} Link Text
Outputs:
<a href=”/” rel=”nofollow”>Link Text</a>
a href=root_path Link Text
Outputs:
<a href=”/”>Link Text</a>
Syntax (cont.)
Text Interpolation:
Slim allows the use of Ruby string interpolation
in attribute values as well as in the text
content.
img src=”/images/example.png” alt=”Welcome
#{user.name}” /
Outputs:
<img src=”/images/example.png” alt=”Welcome Eric
Mulligan” />
p Welcome #{user.name}
Outputs:
<p>Welcome Eric Mulligan</p>
Syntax (cont.)
➢ Outputs:
○ Pretty HTML in Development.
○ Ugly HTML in Production.
○ Valid XHTML.
➢ Sorts attributes by name.
➢ Automatic HTML escaping by default.
➢ Unicode support for element and attribute names.
➢ Support for Embedded Engines such as Ruby,
CoffeeScript, JavaScript, Markdown, etc.
Features
➢ To configure:
○ Create slim.rb in “config/initializers”.
○ Slim::Engine.set_options({...configuration hash…})
○ shortcut: { ‘@’ => { attr: ‘role’ } }:
■ p#example.slim-example@admin Some text
■ Outputs: <p id=”example” class=”slim-example” role=”admin”>Some Text</p>
○ format: :html:
■ Outputs HTML 5 markup instead of the default XHTML.
○ pretty: true:
■ Outputs pretty HTML instead of ugly HTML regardless of the environment.
Features (cont.)
➢ Embedded Ruby (ERB):
○ Ruby code is embedded in HTML markup.
○ Easier to use in projects where web designers have little to no
coding experience.
○ Faster to compile than Slim.
○ Recommended by David Heinemeier Hansson (aka DHH)
(Rails Creator & Basecamp Founder).
Slim vs ERB & HAML
➢ HTML Abstraction Markup Language (HAML):
○ Created by Hampton Catlin (Sass) in 2006.
○ Slim is influenced by HAML.
○ Slight syntax difference between HAML and Slim.
■ HTML/XML elements start with %
● %html, %body, %h1, etc.
■ () used for HTML-style attributes.
● %a(href=”http://google.com”)
Slim vs ERB & HAML (cont.)
Bootstrap Navbar in ERB
Bootstrap Navbar in HAML
Bootstrap Navbar in Slim
➢ Performance:
○ Slim is much faster to compile than HAML.
○ ERB is much faster to compile than Slim & HAML.
Slim vs ERB & HAML (cont.)
Ruby / Rails Perspective
Demo
References:
http://slim-lang.com
http://github.com/slim-lang/slim
http://rubydoc.info/gems/slim/frames
http://sephinrothcn.wordpress.com/2014/04/14/slim-vs-haml-performance-perspective
http://haml.info
Contact Me:
Email: eric.pierre.mulligan@gmail.com
Twitter: @EricPMulligan
Github: https://github.com/EricPMulligan
That’s It!

More Related Content

What's hot

Namespace less engine
Namespace less engineNamespace less engine
Namespace less engineshaokun
 
php app development 1
php app development 1php app development 1
php app development 1barryavery
 
Build a game with javascript (may 21 atlanta)
Build a game with javascript (may 21 atlanta)Build a game with javascript (may 21 atlanta)
Build a game with javascript (may 21 atlanta)Thinkful
 
PHP7.1 New Features & Performance
PHP7.1 New Features & PerformancePHP7.1 New Features & Performance
PHP7.1 New Features & PerformanceXinchen Hui
 
Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8Bram Goffings
 
JavaScript Introduction
JavaScript IntroductionJavaScript Introduction
JavaScript IntroductionJainul Musani
 
SUGUK Cambridge - Display Templates & JSLink for IT Pros
SUGUK Cambridge - Display Templates & JSLink for IT ProsSUGUK Cambridge - Display Templates & JSLink for IT Pros
SUGUK Cambridge - Display Templates & JSLink for IT ProsPaul Hunt
 
JavaScript operators
JavaScript operatorsJavaScript operators
JavaScript operatorsVivek Kumar
 
Client side scripting using Javascript
Client side scripting using JavascriptClient side scripting using Javascript
Client side scripting using JavascriptBansari Shah
 
Spsbe using js-linkanddisplaytemplates
Spsbe   using js-linkanddisplaytemplatesSpsbe   using js-linkanddisplaytemplates
Spsbe using js-linkanddisplaytemplatesPaul Hunt
 

What's hot (20)

Namespace less engine
Namespace less engineNamespace less engine
Namespace less engine
 
Javascript 01 (js)
Javascript 01 (js)Javascript 01 (js)
Javascript 01 (js)
 
HTML Introduction
HTML IntroductionHTML Introduction
HTML Introduction
 
php app development 1
php app development 1php app development 1
php app development 1
 
Build a game with javascript (may 21 atlanta)
Build a game with javascript (may 21 atlanta)Build a game with javascript (may 21 atlanta)
Build a game with javascript (may 21 atlanta)
 
JavaScript - Part-1
JavaScript - Part-1JavaScript - Part-1
JavaScript - Part-1
 
01 basics
01 basics01 basics
01 basics
 
php basic
php basicphp basic
php basic
 
Java server pages
Java server pagesJava server pages
Java server pages
 
Client side scripting
Client side scriptingClient side scripting
Client side scripting
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
PHP7.1 New Features & Performance
PHP7.1 New Features & PerformancePHP7.1 New Features & Performance
PHP7.1 New Features & Performance
 
Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8Dev days Szeged 2014: Plugin system in drupal 8
Dev days Szeged 2014: Plugin system in drupal 8
 
JavaScript Introduction
JavaScript IntroductionJavaScript Introduction
JavaScript Introduction
 
SUGUK Cambridge - Display Templates & JSLink for IT Pros
SUGUK Cambridge - Display Templates & JSLink for IT ProsSUGUK Cambridge - Display Templates & JSLink for IT Pros
SUGUK Cambridge - Display Templates & JSLink for IT Pros
 
JavaScript operators
JavaScript operatorsJavaScript operators
JavaScript operators
 
Client side scripting using Javascript
Client side scripting using JavascriptClient side scripting using Javascript
Client side scripting using Javascript
 
Ph pbasics
Ph pbasicsPh pbasics
Ph pbasics
 
Beanshell scripting in Apache JMeter
Beanshell scripting in Apache JMeterBeanshell scripting in Apache JMeter
Beanshell scripting in Apache JMeter
 
Spsbe using js-linkanddisplaytemplates
Spsbe   using js-linkanddisplaytemplatesSpsbe   using js-linkanddisplaytemplates
Spsbe using js-linkanddisplaytemplates
 

Similar to The Skinny on Slim Template Engine

Killing the Angle Bracket
Killing the Angle BracketKilling the Angle Bracket
Killing the Angle Bracketjnewmanux
 
Road to sbt 1.0 paved with server
Road to sbt 1.0   paved with serverRoad to sbt 1.0   paved with server
Road to sbt 1.0 paved with serverEugene Yokota
 
Angular JS in 2017
Angular JS in 2017Angular JS in 2017
Angular JS in 2017Ayush Sharma
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)Ary Borenszweig
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)Ary Borenszweig
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)Crystal Language
 
Intro to mobile web application development
Intro to mobile web application developmentIntro to mobile web application development
Intro to mobile web application developmentzonathen
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2Rory Gianni
 
Web Fundamentals Crash Course
Web Fundamentals Crash CourseWeb Fundamentals Crash Course
Web Fundamentals Crash CourseMrAbbas
 
Web Fundamentals Crash Course
Web Fundamentals Crash CourseWeb Fundamentals Crash Course
Web Fundamentals Crash CourseMrAbas
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Modelchomas kandar
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Modelchomas kandar
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScriptJorg Janke
 
PyconIE 2016 - Kajiki, the fast and validated template engine your were looki...
PyconIE 2016 - Kajiki, the fast and validated template engine your were looki...PyconIE 2016 - Kajiki, the fast and validated template engine your were looki...
PyconIE 2016 - Kajiki, the fast and validated template engine your were looki...Alessandro Molina
 
A intro to (hosted) Shiny Apps
A intro to (hosted) Shiny AppsA intro to (hosted) Shiny Apps
A intro to (hosted) Shiny AppsDaniel Koller
 

Similar to The Skinny on Slim Template Engine (20)

Killing the Angle Bracket
Killing the Angle BracketKilling the Angle Bracket
Killing the Angle Bracket
 
HTML literals, the JSX of the platform
HTML literals, the JSX of the platformHTML literals, the JSX of the platform
HTML literals, the JSX of the platform
 
Road to sbt 1.0 paved with server
Road to sbt 1.0   paved with serverRoad to sbt 1.0   paved with server
Road to sbt 1.0 paved with server
 
JavaScripts & jQuery
JavaScripts & jQueryJavaScripts & jQuery
JavaScripts & jQuery
 
Angular JS in 2017
Angular JS in 2017Angular JS in 2017
Angular JS in 2017
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)
 
Intro to mobile web application development
Intro to mobile web application developmentIntro to mobile web application development
Intro to mobile web application development
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
 
Web Fundamentals Crash Course
Web Fundamentals Crash CourseWeb Fundamentals Crash Course
Web Fundamentals Crash Course
 
Web Fundamentals Crash Course
Web Fundamentals Crash CourseWeb Fundamentals Crash Course
Web Fundamentals Crash Course
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScript
 
Knolx session
Knolx sessionKnolx session
Knolx session
 
PyconIE 2016 - Kajiki, the fast and validated template engine your were looki...
PyconIE 2016 - Kajiki, the fast and validated template engine your were looki...PyconIE 2016 - Kajiki, the fast and validated template engine your were looki...
PyconIE 2016 - Kajiki, the fast and validated template engine your were looki...
 
A intro to (hosted) Shiny Apps
A intro to (hosted) Shiny AppsA intro to (hosted) Shiny Apps
A intro to (hosted) Shiny Apps
 
topic_perlcgi
topic_perlcgitopic_perlcgi
topic_perlcgi
 
topic_perlcgi
topic_perlcgitopic_perlcgi
topic_perlcgi
 

Recently uploaded

Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 

Recently uploaded (20)

Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 

The Skinny on Slim Template Engine

  • 1. The Skinny On Slim Presentation by Eric Mulligan
  • 2. ➢ What Is Slim? ➢ Why Slim? ➢ Syntax ➢ Features ➢ Slim vs HAML & ERB ➢ Demo Outline
  • 3. ➢ A template engine for Ruby. ➢ Developed by Daniel Mendler in 2010. ➢ Similar to HAML & Jade (JavaScript). ➢ Supports Rails 3 & 4, Sinatra and other Rack-based web frameworks. ➢ Tested on all major Ruby implementations. What Is Slim?
  • 4. ➢ Core syntax is guided by one thought: “What is the minimum required to make this work?” ➢ Allows us to write minimal templates. ➢ Syntax is aesthetically pleasing which makes it fun to write templates. ➢ Designed with performance in mind. ➢ Guarantees well-formed HTML and XML. ➢ Faster to compile than HAML. Why Slim?
  • 5. Installation: From the command line: From a Gemfile: Verbatim Text: ‘|’ tells Slim to copy the line and does no processing. You can even embed HTML and it will simply output it as is. Syntax gem install slim gem ‘slim’ | This is some random text. Outputs: This is some random text. | <p>A paragraph.</p> Outputs: <p>A paragraph</p>
  • 6. Control Code: The ‘-’ denotes control code such as conditionals and loops. ‘end’ is forbidden as blocks are defined by indentation only. Dynamic Content: The ‘=’ outputs the result of the Ruby call and adds the output to the buffer. The ‘==’ is the same as ‘=’, however, all HTML is not escaped which is perfect for ‘yield’ and ‘render’ method calls. - if @users.empty? - @users.each do |user| | I am a user. = javascript_include_tag ‘application’ p = link_to ‘Link’, ‘#’ == yield == render ‘partials/form’, locals: { user: @user } Syntax (cont.)
  • 7. Comments: ‘/’ does not output any markup. ‘/!’, however, outputs HTML comments. ‘/[if IE]’ outputs IE conditional comments. Syntax (cont.) / This line won’t be displayed. /! This line will be displayed as an HTML comment. Outputs: <!-- This line will be displayed as an HTML comment → /[if IE] p Some paragraph. Outputs: <--![if IE]--><p>Some paragraph</p><![endif]-->
  • 8. DOCTYPE: HTML doctypes: XML doctypes: doctype html doctype 5 Outputs: <!DOCTYPE html> doctype 1.1 Outputs: <!DOCTYPE html PUBLIC - “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> doctype xml Outputs: <?xml version=”1.0” encoding=”UTF-8” ?> Syntax (cont.)
  • 9. Inline Tags: Adding ‘:’ between tags allows us to add multiple tags in one single line. Attributes: Wrapping attributes is not required, but it does improve readability. We can wrap attributes with ‘{}’, ‘()’ and ‘[]’. ul.alert.alert-danger: li: a href=”/” First link Outputs: <ul class=”alert alert-danger”> <li><a href=”/”>First link</a></li> </ul> a href=”/” rel=”nofollow” Link Text a[href=”/” rel=”nofollow”] Link Text a(href=”/” rel=”nofollow”) Link Text a{href=”/” rel=”nofollow”} Link Text Outputs: <a href=”/” rel=”nofollow”>Link Text</a> Syntax (cont.)
  • 10. Attributes (cont.): Slim permits us to wrap attributes with ‘*{}’ which allows us to use a Ruby Hash-style syntax. Slim allows attribute values to be Ruby code that outputs a string. a*{:href => ‘/’, rel: ‘nofollow’} Link Text Outputs: <a href=”/” rel=”nofollow”>Link Text</a> a href=root_path Link Text Outputs: <a href=”/”>Link Text</a> Syntax (cont.)
  • 11. Text Interpolation: Slim allows the use of Ruby string interpolation in attribute values as well as in the text content. img src=”/images/example.png” alt=”Welcome #{user.name}” / Outputs: <img src=”/images/example.png” alt=”Welcome Eric Mulligan” /> p Welcome #{user.name} Outputs: <p>Welcome Eric Mulligan</p> Syntax (cont.)
  • 12. ➢ Outputs: ○ Pretty HTML in Development. ○ Ugly HTML in Production. ○ Valid XHTML. ➢ Sorts attributes by name. ➢ Automatic HTML escaping by default. ➢ Unicode support for element and attribute names. ➢ Support for Embedded Engines such as Ruby, CoffeeScript, JavaScript, Markdown, etc. Features
  • 13. ➢ To configure: ○ Create slim.rb in “config/initializers”. ○ Slim::Engine.set_options({...configuration hash…}) ○ shortcut: { ‘@’ => { attr: ‘role’ } }: ■ p#example.slim-example@admin Some text ■ Outputs: <p id=”example” class=”slim-example” role=”admin”>Some Text</p> ○ format: :html: ■ Outputs HTML 5 markup instead of the default XHTML. ○ pretty: true: ■ Outputs pretty HTML instead of ugly HTML regardless of the environment. Features (cont.)
  • 14. ➢ Embedded Ruby (ERB): ○ Ruby code is embedded in HTML markup. ○ Easier to use in projects where web designers have little to no coding experience. ○ Faster to compile than Slim. ○ Recommended by David Heinemeier Hansson (aka DHH) (Rails Creator & Basecamp Founder). Slim vs ERB & HAML
  • 15. ➢ HTML Abstraction Markup Language (HAML): ○ Created by Hampton Catlin (Sass) in 2006. ○ Slim is influenced by HAML. ○ Slight syntax difference between HAML and Slim. ■ HTML/XML elements start with % ● %html, %body, %h1, etc. ■ () used for HTML-style attributes. ● %a(href=”http://google.com”) Slim vs ERB & HAML (cont.)
  • 19. ➢ Performance: ○ Slim is much faster to compile than HAML. ○ ERB is much faster to compile than Slim & HAML. Slim vs ERB & HAML (cont.)
  • 20. Ruby / Rails Perspective
  • 21. Demo