Class: Markaby::Builder

Class | Markaby::Builder
In: | lib/markaby/builder.rb lib/markaby/rails.rb
Parent: | Object

The Markaby::Builder class is the central gear in the system. When using from Ruby code, this is the only class you need to instantiate directly.

      mab = Markaby::Builder.new
      mab.html do
        head { title "Boats.com" }
        body do
          h1 "Boats.com has great deals"
          ul do
            li "$49 for a canoe"
            li "$39 for a raft"
            li "$29 for a huge boot that floats and can fit 5 people"
          end
        end
      end
      puts mab.to_s

### Methods

<< _erbout _erbout capture concat content_for head html_tag ignore_helpers ignored_helpers method_missing new set tag! text to_s xhtml_strict xhtml_transitional

### Attributes

output_helpers | [RW] |
tagset | [RW] |

### Public Class methods

ignore_helpers(*helpers)
ignored_helpers()
new(assigns = {}, helpers = nil, &block)

Create a Markaby builder object. Pass in a hash of variable assignments to `assigns` which will be available as instance variables inside tag construction blocks. If an object is passed in to `helpers`, its methods will be available from those same blocks.

Pass in a `block` to new and the block will be evaluated.

      mab = Markaby::Builder.new {
        html do
          body do
            h1 "Matching Mole"
          end
        end
      }

set(option, value)

### Public Instance methods

<<(string)

Alias for text

_erbout()

Emulate ERB to satisfy helpers like `form_for`.

_erbout()

Emulate ERB to satisfy helpers like `form_for`.

capture(&block)

Captures the HTML code built inside the `block`. This is done by creating a new stream for the builder object, running the block and passing back its stream as a string.

      >> Markaby::Builder.new.capture { h1 "TEST"; h2 "CAPTURE ME" }
      => "<h1>TITLE</h1>\n<h2>CAPTURE ME</h2>\n"

concat(string)

Alias for text

content_for(name, &block)

Content_for will store the given block in an instance variable for later use in another template or in the layout.

The name of the instance variable is content_for_<name> to stay consistent with @content_for_layout which is used by ActionView‘s layouts.

Example:

      content_for("header") do
        h1 "Half Shark and Half Lion"
      end

If used several times, the variable will contain all the parts concatenated.

head(*args, &block)

Builds a head tag. Adds a `meta` tag inside with Content-Type set to `text/html; charset=utf-8`.

html_tag(sym, *args, &block)

Every HTML tag method goes through an html_tag call. So, calling `div` is equivalent to calling `html_tag(:div)`. All HTML tags in Markaby‘s list are given generated wrappers for this method.

If the @auto_validation setting is on, this method will check for many common mistakes which could lead to invalid XHTML.

method_missing(sym, *args, &block)

This method is used to intercept calls to helper methods and instance variables. Here is the order of interception:

- If `sym` is a helper method, the helper method is called and output to the stream.
- If `sym` is a Builder::XmlMarkup method, it is passed on to the builder object.
- If `sym` is also the name of an instance variable, the value of the instance variable is returned.
- If `sym` has come this far and no `tagset` is found, `sym` and its arguments are passed to tag!
- If a tagset is found, though, `NoMethodError` is raised.

method_missing used to be the lynchpin in Markaby, but it‘s no longer used to handle HTML tags. See html_tag for that.

tag!(tag, *args, &block)

Create a tag named `tag`. Other than the first argument which is the tag name, the arguments are the same as the tags implemented via method_missing.

text(string)

Write a `string` to the HTML stream without escaping it.

to_s()

Returns a string containing the HTML stream. Internally, the stream is stored as an Array.

xhtml_strict(&block)

Builds an html tag with XHTML 1.0 Strict doctype instead.

xhtml_transitional(&block)

Builds an html tag. An XML 1.0 instruction and an XHTML 1.0 Transitional doctype are prepended. Also assumes `:xmlns => "www.w3.org/1999/xhtml", :lang => "en"`.

[Validate]