| Class | Builder::XmlMarkup |
| In: |
vendor/rails/actionpack/lib/action_view/vendor/builder/xmlmarkup.rb
|
| Parent: | XmlBase |
Create XML markup easily. All (well, almost all) methods sent to an XmlMarkup object will be translated to the equivalent XML markup. Any method with a block will be treated as an XML markup tag with nested markup in the block.
Examples will demonstrate this easier than words. In the following, xm is an XmlMarkup object.
xm.em("emphasized") # => <em>emphasized</em>
xm.em { xmm.b("emp & bold") } # => <em><b>emph & bold</b></em>
xm.a("A Link", "href"=>"http://onestepback.org")
# => <a href="http://onestepback.org">A Link</a>
xm.div { br } # => <div><br/></div>
xm.target("name"=>"compile", "option"=>"fast")
# => <target option="fast" name="compile"\>
# NOTE: order of attributes is not specified.
xm.instruct! # <?xml version="1.0" encoding="UTF-8"?>
xm.html { # <html>
xm.head { # <head>
xm.title("History") # <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>
Example:
xm.div { # <div>
xm.text! "line"; xm.br # line<br/>
xm.text! "another line"; xmbr # another line<br/>
} # </div>
Example:
xml.tag!("SOAP:Envelope") { ... }
will produce …
<SOAP:Envelope> ... </SOAP:Envelope>"
tag! will also take text and attribute arguments (after the tag name) like normal markup methods. (But see the next bullet item for a better way to handle XML namespaces).
xml.SOAP :Envelope do ... end
Just put a space before the colon in a namespace to produce the right form for builder (e.g. "SOAP:Envelope" => "xml.SOAP :Envelope")
Examples:
xm = Builder::XmlMarkup.new
result = xm.title("yada")
# result is a string containing the markup.
buffer = ""
xm = Builder::XmlMarkup.new(buffer)
# The markup is appended to buffer (using <<)
xm = Builder::XmlMarkup.new(STDOUT)
# The markup is written to STDOUT (using <<)
xm = Builder::XmlMarkup.new
x2 = Builder::XmlMarkup.new(:target=>xm)
# Markup written to +x2+ will be send to +xm+.
Example:
xm = Builder.new(:ident=>2)
# xm will produce nicely formatted and indented XML.
xm = Builder.new(:indent=>2, :margin=>4)
# xm will produce nicely formatted and indented XML with 2
# spaces per indent and an over all indentation level of 4.
builder = Builder::XmlMarkup.new(:target=>$stdout, :indent=>2)
builder.name { |b| b.first("Jim"); b.last("Weirich) }
# prints:
# <name>
# <first>Jim</first>
# <last>Weirich</last>
# </name>
xml.div { strong("text") }
you need to write:
xml.div { xml.strong("text") }
Although more verbose, the subtle change in semantics within the block was found to be prone to error. To make this change a little less cumbersome, the markup block now gets the markup object sent as an argument, allowing you to use a shorter alias within the block.
For example:
xml_builder = Builder::XmlMarkup.new
xml_builder.div { |xml|
xml.stong("text")
}
Create an XML markup builder. Parameters are specified by an option hash.
| :target=>target_object: | Object receiving the markup. out must respond to the << operator. The default is a plain string target. |
| :indent=>indentation: | Number of spaces used for indentation. The default is no indentation and no line breaks. |
| :margin=>initial_indentation_level: | Amount of initial indentation (specified in levels, not spaces). |
# File vendor/rails/actionpack/lib/action_view/vendor/builder/xmlmarkup.rb, line 177
177: def initialize(options={})
178: indent = options[:indent] || 0
179: margin = options[:margin] || 0
180: super(indent, margin)
181: @target = options[:target] || ""
182: end
Surrounds the given text with a CDATA tag
For example:
xml.cdata! "blah blah blah"
# => <![CDATA[blah blah blah]]>
# File vendor/rails/actionpack/lib/action_view/vendor/builder/xmlmarkup.rb, line 250
250: def cdata!(text)
251: _ensure_no_block block_given?
252: _special("<![CDATA[", "]]>", text, nil)
253: end
# File vendor/rails/actionpack/lib/action_view/vendor/builder/xmlmarkup.rb, line 189
189: def comment!(comment_text)
190: _ensure_no_block block_given?
191: _special("<!-- ", " -->", comment_text, nil)
192: end
Insert an XML declaration into the XML markup.
For example:
xml.declare! :ELEMENT, :blah, "yada"
# => <!ELEMENT blah "yada">
# File vendor/rails/actionpack/lib/action_view/vendor/builder/xmlmarkup.rb, line 200
200: def declare!(inst, *args, &block)
201: _indent
202: @target << "<!#{inst}"
203: args.each do |arg|
204: case arg
205: when String
206: @target << %{ "#{arg}"}
207: when Symbol
208: @target << " #{arg}"
209: end
210: end
211: if block_given?
212: @target << " ["
213: _newline
214: _nested_structures(block)
215: @target << "]"
216: end
217: @target << ">"
218: _newline
219: end
Insert a processing instruction into the XML markup. E.g.
For example:
xml.instruct!
#=> <?xml version="1.0" encoding="UTF-8"?>
xml.instruct! :aaa, :bbb=>"ccc"
#=> <?aaa bbb="ccc"?>
# File vendor/rails/actionpack/lib/action_view/vendor/builder/xmlmarkup.rb, line 230
230: def instruct!(directive_tag=:xml, attrs={})
231: _ensure_no_block block_given?
232: if directive_tag == :xml
233: a = { :version=>"1.0", :encoding=>"UTF-8" }
234: attrs = a.merge attrs
235: end
236: _special(
237: "<?#{directive_tag}",
238: "?>",
239: nil,
240: attrs,
241: [:version, :encoding, :standalone])
242: end