Class: Markaby::Builder

### Home Classes Methods

### In Files

- lib/markaby/builder.rb

### Parent

Object

### Methods

- ::get
- ::ignore_helpers
- ::ignored_helpers
- ::new
- ::restore_defaults!
- ::set
- #<<
- #capture
- #concat
- #helper=
- #locals=
- #tag!
- #text
- #to_s

### Included Modules

- Markaby::BuilderTags

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Markaby
- Markaby::Builder
- Markaby::BuilderTags
- Markaby::CssProxy
- Markaby::Fragment
- Markaby::InvalidXhtmlError
- Markaby::Rails
- Markaby::Rails::ActionControllerHelpers
- Markaby::Rails::Helpers
- Markaby::Rails::RailsBuilder
- Markaby::Rails::RailsBuilder::FormHelperProxy
- Markaby::Rails::RailsBuilder::OutputBuffer
- Markaby::Rails::Template
- Markaby::Rails::TemplateHandler
- Markaby::Stream
- Markaby::Tilt
- Markaby::XHTMLFrameset
- Markaby::XHTMLStrict
- Markaby::XHTMLTransitional
- Markaby::XmlMarkup
- Sinatra
- Sinatra::Base
- Sinatra::Templates
- Kernel
- Object

No matching classes.

# Markaby::Builder

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

### Constants

DEFAULT_OPTIONS

### Attributes

output_helpers[RW]
tagset[RW]

### Public Class Methods

get(option) click to toggle source

    # File lib/markaby/builder.rb, line 51
    def self.get(option)
      @@options[option]
    end

ignore_helpers(*helpers) click to toggle source

    # File lib/markaby/builder.rb, line 59
    def self.ignore_helpers(*helpers)
      ignored_helpers.concat helpers
    end

ignored_helpers() click to toggle source

    # File lib/markaby/builder.rb, line 55
    def self.ignored_helpers
      @@ignored_helpers ||= []
    end

new(assigns = {}, helper = nil, &block) click to toggle source

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 `helper`, 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
    }

    # File lib/markaby/builder.rb, line 80
    def initialize(assigns = {}, helper = nil, &block)
      @streams = [Stream.new]
      @assigns = assigns.dup
      @_helper = helper
      @used_ids = {}

      @@options.each do |k, v|
        instance_variable_set("@#{k}", @assigns.delete(k) || v)
      end

      @assigns.each do |k, v|
        instance_variable_set("@#{k}", v)
      end

      if helper
        helper.instance_variables.each do |iv|
          instance_variable_set(iv, helper.instance_variable_get(iv))
        end
      end

      @builder = XmlMarkup.new(:indent => @indent, :target => @streams.last)

      text(capture(&block)) if block
    end

restore_defaults!() click to toggle source

    # File lib/markaby/builder.rb, line 43
    def self.restore_defaults!
      @@options = DEFAULT_OPTIONS.dup
    end

set(option, value) click to toggle source

    # File lib/markaby/builder.rb, line 47
    def self.set(option, value)
      @@options[option] = value
    end

### Public Instance Methods

<<(string) click to toggle source
Alias for: text
capture(&block) click to toggle source

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>TEST</h1><h2>CAPTURE ME</h2>"

    # File lib/markaby/builder.rb, line 145
    def capture(&block)
      @streams.push(@builder.target = Stream.new)
      @builder.level += 1
      str = instance_eval(&block)
      str = @streams.last.join if @streams.last.any?
      @streams.pop
      @builder.level -= 1
      @builder.target = @streams.last
      str
    end

concat(string) click to toggle source
Alias for: text
helper=(helper) click to toggle source

    # File lib/markaby/builder.rb, line 105
    def helper=(helper)
      @_helper = helper
    end

locals=(locals) click to toggle source

    # File lib/markaby/builder.rb, line 116
    def locals=(locals)
      locals.each do |key, value|
        metaclass do
          define_method key do
            value
          end
        end
      end
    end

tag!(tag, *args, &block) click to toggle source

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.

    # File lib/markaby/builder.rb, line 158
    def tag!(tag, *args, &block)
      ele_id = nil
      if @auto_validation && @tagset
        if !@tagset.tagset.has_key?(tag)
          raise InvalidXhtmlError, "no element `#{tag}' for #{tagset.doctype}"
        elsif args.last.respond_to?(:to_hash)
          attrs = args.last.to_hash

          if @tagset.forms.include?(tag) && attrs[:id]
            attrs[:name] ||= attrs[:id]
          end

          attrs.each do |k, v|
            atname = k.to_s.downcase.intern
            unless k =~ /:/ or @tagset.tagset[tag].include? atname
              raise InvalidXhtmlError, "no attribute `#{k}' on #{tag} elements"
            end
            if atname == :id
              ele_id = v.to_s
              if @used_ids.has_key? ele_id
                raise InvalidXhtmlError, "id `#{ele_id}' already used (id's must be unique)."
              end
            end
            if AttrsBoolean.include? atname
              if v
                attrs[k] = atname.to_s
              else
                attrs.delete k
              end
            end
          end
        end
      end

      if block
        str = capture(&block)
        block = proc { text(str) }
      end

      f = fragment { @builder.tag!(tag, *args, &block) }
      @used_ids[ele_id] = f if ele_id
      f
    end

text(string) click to toggle source

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

    # File lib/markaby/builder.rb, line 132
    def text(string)
      @builder << string.to_s
      nil
    end

Also aliased as: <<, concat
to_s() click to toggle source

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

    # File lib/markaby/builder.rb, line 127
    def to_s
      @streams.last.to_s
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.