< Ruby on Rails < ActionView

XmlBuilder is a class which supports creating XML output with Ruby methods. Most method calls sent to the XmlBuilder class will be rendered directly. You also have access to all instance variables defined in the action. For example:

In /app/controllers/demo_controller.rb:

 class DemoController < ApplicationController
   def index
     @title = 'History'
   end
 end

In /app/views/demo/index.rxml:

  xm.instruct!                   # <?xml version="1.0" encoding="UTF-8"?>
  xm.html {                      # <html>
    xm.head {                    #   <head>
      xm.title(@title)        #     <title>History</title>
    }                            #   </head>
    xm.body {                    #   <body>
      xm.comment! "HI"           #     <!-- HI -->
      xm.h1("Header")            #     <h1>Header</h1>
      xm.p("paragraph")          #     <p>paragraph</p>
    }                            #   </body>
  }                              # </html>

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.