Module: Forma::Html

Included in:
Action, Field, Form, Table
Defined in:
lib/forma/html.rb

Overview

Utilities for html text generation.

Defined Under Namespace

Classes: Attr, ClassAttr, Element, SimpleAttr, StyleAttr

Class Method Summary collapse

Class Method Details

.attr(*params) ⇒ Object

Attribute creation.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/forma/html.rb', line 7

def attr(*params)
  if params.length == 2
    SimpleAttr.new(params[0].to_s, params[1].to_s)
  elsif params.length == 1 and params[0].is_a?(Hash)
    StyleAttr.new(params[0])
  elsif params.length == 1
    ClassAttr.new(params[0])
  else
    raise "illegal attr specification: #{params}"
  end
end

.el(tag, opts = {}) ⇒ Object

You can easily create elements using this method.

“‘ include Forma::Html element = el(“div”, attrs = ’main’, class: ‘header’, style: {‘font-size’ => ‘20px’}) html = element.to_s “‘



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/forma/html.rb', line 26

def el(tag, opts = {})
  opts = opts.symbolize_keys
  h = { text: opts[:text], html: opts[:html] }
  if opts[:attrs]
    attributes = []
    opts[:attrs].each do |k, v|
      if k == :class || k == :style
        attributes << attr(v)
      else
        attributes << attr(k, v)
      end
    end
    h[:attrs] = attributes
  end
  h[:children] = opts[:children]
  Element.new(tag, h)
end