Using the Tilt gem to call a templating engine

require 'haml'
require 'tilt'

s = "!!!
%html
  %head
    %title Haml on Sinatra Example
  %body
"
template = Tilt['haml'].new({pretty: true}) {|x| s}
output = template.render
puts output

Output

<!DOCTYPE html>

<html>
  <head>
    <title>Haml on Sinatra Example</title>
  </head>
  <body></body>
</html>

.. . and with Slim:

require 'slim'
require 'tilt'

author = 'James Robertson'
s =<<EOF
doctype html
html
  head
    title Slim Examples
    meta name="keywords" content="template language"
    meta name="author" content="#{author}"
EOF

template = Tilt['slim'].new({pretty: true}) {|x| s}
output = template.render
 puts output

Output

<!D

OCTYPE html>

<html>
  <head>
    <title>
      Slim Examples
    </title>
    <meta content="template language" name="keywords" />
    <meta content="James Robertson" name="author" />
  </head>
</html>

Resources

Tags:
Source:
2101hrs.txt
Published:
17-08-2013 21:01