data & special symbols")
xml.target!
#=> " data & special symbols]]>"
# CDATA with script content
xml = Builder::XmlMarkup.new(:indent => 2)
xml.html do |x|
x.head do
x.script(:type => "text/javascript") do
x.cdata!("function test() { if (a < b && c > d) { return true; } }")
end
end
end
puts xml.target!
# Output:
#
#
#
#
#
# CDATA with embedded close sequence (automatically escaped)
xml = Builder::XmlMarkup.new
xml.cdata!("Content with ]]> embedded")
xml.target!
#=> " embedded]]>"
```
--------------------------------
### Generate XML Comments in Ruby
Source: https://github.com/jimweirich/builder/blob/master/README.md
Demonstrates how to add XML comments to the generated markup using the `comment!` method. This is useful for adding explanatory notes within the XML structure.
```ruby
xml_markup.comment! "This is a comment"
#=>
```
--------------------------------
### Method-Based Tag Generation
Source: https://context7.com/jimweirich/builder/llms.txt
Illustrates how Builder automatically converts Ruby method calls into XML tags, including simple tags, tags with attributes, and nested structures using blocks.
```APIDOC
## Method-Based Tag Generation
### Description
Any method called on the builder that isn't a reserved method is automatically converted to an XML tag. Pass content as the first argument, attributes as a hash, and nest elements using blocks.
### Method
`builder_instance.tag_name(content = nil, attributes = {}, &block)`
### Parameters
- **tag_name** (Symbol or String) - The name of the XML tag to create.
- **content** (String, optional) - The text content of the tag.
- **attributes** (Hash, optional) - A hash of attributes for the tag.
- **block** (Proc, optional) - A block to define nested elements.
### Request Example
```ruby
require 'builder'
xml = Builder::XmlMarkup.new(:indent => 2)
# Simple tags
xml.simple
#=> ""
xml = Builder::XmlMarkup.new
xml.greeting("Hello")
puts xml.target!
#=> "Hello"
# Tags with attributes
xml = Builder::XmlMarkup.new
xml.a("Click here", :href => "http://example.com", :target => "_blank")
puts xml.target!
#=> "Click here"
# Nested tags with block syntax
xml = Builder::XmlMarkup.new(:indent => 2)
xml.html do |x|
x.head do
x.title("My Page")
x.meta(:charset => "UTF-8")
end
x.body do
x.h1("Welcome")
x.p("This is a paragraph.")
x.ul do
x.li("Item 1")
x.li("Item 2")
x.li("Item 3")
end
end
end
puts xml.target!
# Output:
#
#
# My Page
#
#
#
# Welcome
# This is a paragraph.
#
# - Item 1
# - Item 2
# - Item 3
#
#
#
```
### Response
#### Success Response (200)
- **String**: The generated XML markup.
#### Response Example
```json
{
"example": "\n \n My Page\n \n \n \n Welcome
\n This is a paragraph.
\n \n - Item 1
\n - Item 2
\n - Item 3
\n
\n \n"
}
```
```
--------------------------------
### Builder::XmlEvents SAX-like Generation
Source: https://context7.com/jimweirich/builder/llms.txt
Explains how to use XmlEvents to generate SAX-style events instead of direct string output, allowing for custom processing logic.
```APIDOC
## Builder::XmlEvents
### Description
Creates SAX-like XML events. Events are dispatched to a handler object that must implement `start_tag`, `end_tag`, and `text` methods.
### Method
N/A (Event-driven)
### Parameters
- **target** (Object) - The handler object that receives the events.
### Response
- **events** (Array) - A sequence of event objects containing type, tag, and attribute data.
```
--------------------------------
### Generate XML Tags using Method-Based Syntax
Source: https://context7.com/jimweirich/builder/llms.txt
Leverages Ruby's method_missing to convert method calls into XML tags. Content can be passed as the first argument, attributes as a hash, and nested elements can be defined using block syntax.
```ruby
require 'builder'
xml = Builder::XmlMarkup.new(:indent => 2)
# Simple tags
xml.simple
#=> ""
xml = Builder::XmlMarkup.new
xml.greeting("Hello")
xml.target!
#=> "Hello"
# Tags with attributes
xml = Builder::XmlMarkup.new
xml.a("Click here", :href => "http://example.com", :target => "_blank")
xml.target!
#=> "Click here"
# Nested tags with block syntax
xml = Builder::XmlMarkup.new(:indent => 2)
xml.html do |x|
x.head do
x.title("My Page")
x.meta(:charset => "UTF-8")
end
x.body do
x.h1("Welcome")
x.p("This is a paragraph.")
x.ul do
x.li("Item 1")
x.li("Item 2")
x.li("Item 3")
end
end
end
puts xml.target!
# Output:
#
#
# My Page
#
#
#
# Welcome
# This is a paragraph.
#
# - Item 1
# - Item 2
# - Item 3
#
#
#
```
--------------------------------
### Create XML Elements with Dynamic Names using tag!
Source: https://context7.com/jimweirich/builder/llms.txt
Generates an XML tag with a dynamic name, useful for tag names with special characters or runtime-determined names. It accepts text content, attributes, and a block for nested elements.
```ruby
require 'builder'
xml = Builder::XmlMarkup.new
# Tag with special characters in name
xml.tag!("SOAP:Envelope") { |x| x.tag!("SOAP:Body", "content") }
xml.target!
#=> "content"
# Tag with attributes and text
xml = Builder::XmlMarkup.new
xml.tag!("custom-element", "Hello", :id => 123, :class => "highlight")
xml.target!
#=> "Hello"
# Dynamic tag names from variables
xml = Builder::XmlMarkup.new(:indent => 2)
tag_name = "dynamic-tag"
attributes = { :version => "1.0", :type => "custom" }
xml.tag!(tag_name, attributes) do |x|
x.tag!("sub-item", "Value 1")
x.tag!("sub-item", "Value 2")
end
xml.target!
#=> "\n Value 1\n Value 2\n\n"
# Tag with Ruby reserved words
xml = Builder::XmlMarkup.new
xml.tag!("class", :id => 1) { |x| x.tag!("loop", "content") }
xml.target!
#=> "content"
```
--------------------------------
### Appending Unescaped Content with Builder's << Operator
Source: https://context7.com/jimweirich/builder/llms.txt
Demonstrates using the `<<` operator to append raw, unescaped content to the XML output. This is useful for inserting pre-formatted XML snippets or combining output from multiple Builder instances without further escaping.
```ruby
require 'builder'
# Insert raw XML content
xml = Builder::XmlMarkup.new
xml.div do |x|
x << "Pre-formatted HTML"
x.p("Normal content")
end
xml.target!
#=> "Pre-formatted HTMLNormal content
"
# Stacking builders (using one builder as target for another)
outer = Builder::XmlMarkup.new(:indent => 2)
inner = Builder::XmlMarkup.new(:target => outer)
outer.container do
inner.item("First")
inner.item("Second")
end
puts outer.target!
# Output:
#
# - First
# - Second
#
# Combining output from multiple sources
xml = Builder::XmlMarkup.new
header_xml = ""
footer_xml = ""
xml.document do |x|
x << header_xml
x.body { x.content("Main content here") }
x << footer_xml
end
xml.target!
#=> "Main content here"
```
--------------------------------
### Generating SAX-like XML Events
Source: https://context7.com/jimweirich/builder/llms.txt
Uses Builder::XmlEvents to generate XML structures as a stream of events rather than a single string. This requires a handler object that implements start_tag, end_tag, and text methods.
```ruby
require 'builder'
class XmlEventHandler
attr_reader :events
def initialize; @events = []; end
def start_tag(tag, attrs); @events << { type: :start, tag: tag, attrs: attrs }; end
def end_tag(tag); @events << { type: :end, tag: tag }; end
def text(content); @events << { type: :text, content: content }; end
end
handler = XmlEventHandler.new
xe = Builder::XmlEvents.new(:target => handler)
xe.person(:id => 1) do |x|
x.name("John Doe")
x.email("john@example.com")
end
```
--------------------------------
### comment! - Insert XML Comments
Source: https://context7.com/jimweirich/builder/llms.txt
Inserts an XML comment into the markup, automatically wrapping the content in markers.
```APIDOC
## comment! (XML Comment)
### Description
Inserts an XML comment. The content is properly formatted with indentation if configured.
### Parameters
- **text** (String) - Required - The text content of the comment.
### Request Example
xml.comment!("Database configuration")
### Response
- **Output** (String) - ""
```
--------------------------------
### declare! - DTD and Entity Declarations
Source: https://context7.com/jimweirich/builder/llms.txt
Inserts XML declarations for DTDs, elements, entities, and document type definitions.
```APIDOC
## declare! (DTD Declaration)
### Description
Inserts DTD declarations. Symbols are inserted without quotes, strings with double quotes.
### Parameters
- **type** (Symbol) - Required - The declaration type (e.g., :DOCTYPE, :ELEMENT).
- **args** (Array) - Optional - Additional arguments for the declaration.
### Request Example
xml.declare!(:DOCTYPE, :html)
### Response
- **Output** (String) - ""
```
--------------------------------
### Managing XML Attribute Escaping in Builder
Source: https://context7.com/jimweirich/builder/llms.txt
Demonstrates how Builder handles attribute escaping. String values are automatically escaped, while symbols are treated as raw text, allowing for entity references or unescaped content.
```ruby
xml = Builder::XmlMarkup.new
xml.company(:name => "Smith & Sons", :slogan => "Quality \"Guaranteed\"")
xml.target!
xml = Builder::XmlMarkup.new
xml.link(:href => :"&baseurl;/page.html", :title => "Normal \"escaping\"")
xml.target!
xml = Builder::XmlMarkup.new
xml.sample(
:escaped => "AT&T",
:unescaped => :"M&M"
)
xml.target!
xml = Builder::XmlMarkup.new
xml.text(:value => "Line 1\nLine 2\rLine 3")
xml.target!
```
--------------------------------
### Inserting Escaped Text with Builder's text! Method
Source: https://context7.com/jimweirich/builder/llms.txt
Shows how to insert plain text content into an XML structure using the `text!` method. This method automatically escapes XML special characters to ensure valid markup. It's useful for inserting text that might contain characters like '<', '>', or '&'.
```ruby
require 'builder'
# Basic text insertion with escaping
xml = Builder::XmlMarkup.new
xml.div do |x|
x.text!("Price: $10 < $20 & ")
x.strong("save money!")
end
xml.target!
#=> "Price: $10 < $20 & save money!
"
# Mixed content with text and elements
xml = Builder::XmlMarkup.new(:indent => 2)
xml.p do |x|
x.text!("Click ")
x.a("here", :href => "#")
x.text!(" to continue or ")
x.a("cancel", :href => "/cancel")
x.text!(".")
end
xml.target!
#=> "\n Click here\n to continue or cancel\n .\n
\n"
# Building inline formatted text
xml = Builder::XmlMarkup.new
xml.span do |x|
x.text!("The ")
x.em("quick")
x.text!(" brown ")
x.strong("fox")
x.text!(" jumps over the lazy dog.")
end
xml.target!
#=> "The quick brown fox jumps over the lazy dog."
```
--------------------------------
### Builder Gem Attribute Escaping
Source: https://context7.com/jimweirich/builder/llms.txt
Illustrates how Builder automatically escapes string attribute values to ensure valid XML. It also shows how to use symbols for attribute values that should not be escaped, such as XML entity references.
```ruby
require 'builder'
# String attribute values are automatically escaped.
# Use symbols for attribute values that should remain unescaped (e.g., entity references).
xml = Builder::XmlMarkup.new
xml.a("Click here", :href => "http://example.com?q=a&b=c", :title => "Link with < & >")
puts xml.target!
#=> "Click here"
```
--------------------------------
### Attribute Escaping Rules
Source: https://github.com/jimweirich/builder/blob/master/README.md
Details how attribute values are handled in Builder 2.0.0, specifically regarding automatic escaping of strings versus symbols.
```APIDOC
## PUT /Builder::XmlMarkup/Attributes
### Description
Configures how attributes are rendered in XML. String values are automatically escaped, while Symbol values are rendered as-is.
### Method
PUT
### Parameters
#### Request Body
- **attributes** (Hash) - Required - Key-value pairs representing XML attributes.
### Request Example
```ruby
xml.sample(:escaped => "This&That", :unescaped => :"Here&There")
```
### Response
#### Success Response (200)
- **output** (String) - The resulting XML tag with escaped/unescaped attributes.
#### Response Example
```xml
```
```
--------------------------------
### Control attribute escaping in XML
Source: https://github.com/jimweirich/builder/blob/master/README.md
Explains how Builder handles attribute escaping. String values are automatically escaped, while Symbol values remain unescaped.
```ruby
xml = Builder::XmlMarkup.new
xml.sample(:escaped=>"This&That", :unescaped=>:"Here&There")
xml.target! # =>
```
--------------------------------
### cdata! - Insert CDATA Sections
Source: https://context7.com/jimweirich/builder/llms.txt
Inserts a CDATA section for content that should not be parsed as XML, such as scripts or raw text.
```APIDOC
## cdata! (CDATA Section)
### Description
Inserts a CDATA section. Automatically handles escaping of the CDATA close sequence (]]>).
### Parameters
- **content** (String) - Required - The raw content to be wrapped in CDATA.
### Request Example
xml.cdata!("function test() { return true; }")
### Response
- **Output** (String) - ""
```
--------------------------------
### Escape XML Attribute Values in Ruby
Source: https://github.com/jimweirich/builder/blob/master/README.md
Details how Builder automatically escapes string attribute values to prevent XML injection issues. Using symbols for attribute values bypasses this escaping mechanism.
```ruby
xml = Builder::XmlMarkup.new
xml.sample(:escaped=>"This&That", :unescaped=>:"Here&There")
xml.target! =>
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.