# XML in Ruby - Templating Engine

**URL:** <https://www.ruby-forum.com/t/xml-in-ruby-templating-engine/114622>\
**Category:** Ruby\
**Created:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/xml-in-ruby-templating-engine/114622 "2007-09-17T13:23:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaeln](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaeln](https://www.ruby-forum.com/u/michaeln)\
**Post date:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/xml-in-ruby-templating-engine/114622/1 "2007-09-17T13:23:07Z")

</div>

Hi,

While everyone embeds Ruby into HTML code nowadays, I thought why not  
do it the other way round? Vim syntax highlighting included 🙂

[http://www.ntecs.de/hg-projects/xml\_in\_ruby/](http://www.ntecs.de/hg-projects/xml_in_ruby/)

Uh, and it’s soo simple, and you get a lot of goodies for free (for  
example you can split your templates into methods, or inherit from other  
template classes etc.).

Regards,

Michael

* * *

## XML in Ruby - Templating Engine

ABOUT

“XML in Ruby” is a simple templating engine that allows you to embed  
XML directly into Ruby.

EXAMPLE

See sample.rbx.

```
   class MyView < View
     def index(title)
       <html>
         <head>
           <title>${title}</title>
         </head>
         <body>
           render_body
         </body>
       </html>
     end

     def render_body
       output "Hello World"
     end
   end

   puts MyView.render(:index, "Test")

```

To start:

```
   ruby -r view -e "View.require 'sample.rbx'"

```

Output:

```
   <html>
   <head>
   <title>Test</title>
   </head>
   <body>
   Hello World</body>
   </html>

```

You can mix XML and Ruby. Everything that starts with “\<” as  
fist non-whitespace character on a line is considered as XML.  
You can also use:

```
   <: This is text

```

to output the text " This is text", or require other  
“templates” with:

```
   View.require 'another_template.rbx'

```

In XML you can embed Ruby code with #{…} and ${…}. The second  
performs escaping.
