Sha256: 3137da28df6b72b735ee2b89fe10f43ef7827b3d59e7721b85ee708c433bb3a3

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

== Ruby

It may not seem obvious at first, but Ruby itself can be used as
a template system.

Lets say we have a Ruby document called 'test.rb' containing:

  "<h1>Example #{ title }</h1>\n" +
  "<p>This is an example of Ruby rendering.</p>"

We can run this Ruby script thru Malt's +render+ function.

  data = {:title => 'Document'}

  html = Malt.render(:file=>'tmp/test.rb', :data=>data)

Whatever was the final result of evaluating the Ruby script, converted to
a string via #to_s, will be the result of the rendering.

  html.assert.include?('<h1>Example Document</h1>')

We can get a hold of the Ruby template via the Malt.file function.

  ruby = Malt.file('tmp/test.rb')

  ruby.class.assert == Malt::Format::Ruby

Ruby is a <i>universal template format</i>, so it can be converted to any other
format (even if it is not really that format).

  data = {:title => "Document"}

  html = ruby.to_html(data)

First we will notice that the output is an instance of Malt::Format::HTML.

  html.class.assert == Malt::Format::HTML

And that by calling #to_s we can get the rendered HTML document.

  html.to_s.assert.include?('<h1>Example Document</h1>')

Or we can convert the Ruby document directly to HTML via the #html method.

  out = ruby.html(data)

  out.assert.include?('<h1>Example Document</h1>')

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
malt-0.3.0 qed/03_formats/16_ruby.rdoc
malt-0.2.0 qed/03_formats/16_ruby.rdoc
malt-0.1.1 qed/03_formats/16_ruby.rdoc