Class: FunHtml::Template

Inherits:
Object
  • Object
show all
Includes:
SpecElements::HTMLAllElements, Writer
Defined in:
lib/fun_html/template.rb

Overview

FunHtml Template will generate typed HTML. Each tag and attribute has a match method that is typed via Sorbet (which is optional).

The template is designed to allow subclassing or using ‘start` to generate templates without subclassing.

When subclassing understand that ‘new` generatings a “buffer”. Each time a tag(div, b, body, etc) is called it will be added to the buffer. Once `render` is called the buffer is is returned and then cleared.

class Example < FunHtml::Template
  def initialize(name)
    super()
    @name = name
  end

  def view
    doctype
    html do
      body do
        h1 { text @name }
      end
    end
  end
end

puts Example.new('My Example').view.render
<!DOCTYPE html><html><body><h1>My Example</h1></body></html>

If you need to create custom tags, create a method that integrates with the Writer#write method.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SpecElements::HTMLMarqueeElement

#marquee

Methods included from SpecElements::HTMLFontElement

#font

Methods included from SpecElements::HTMLParamElement

#param

Methods included from SpecElements::HTMLFrameSetElement

#frameset

Methods included from SpecElements::HTMLFrameElement

#frame

Methods included from SpecElements::HTMLDirectoryElement

#dir

Methods included from SpecElements::HTMLUnknownElement

#applet, #bgsound, #blink, #isindex, #keygen, #multicol, #nextid, #spacer

Methods included from SpecElements::HTMLCanvasElement

#canvas

Methods included from SpecElements::HTMLSlotElement

#slot

Methods included from SpecElements::HTMLTemplateElement

#template

Methods included from SpecElements::HTMLDialogElement

#dialog

Methods included from SpecElements::HTMLDetailsElement

#details

Methods included from SpecElements::HTMLLegendElement

#legend

Methods included from SpecElements::HTMLFieldSetElement

#fieldset

Methods included from SpecElements::HTMLMeterElement

#meter

Methods included from SpecElements::HTMLProgressElement

#progress

Methods included from SpecElements::HTMLOutputElement

#output

Methods included from SpecElements::HTMLTextAreaElement

#textarea

Methods included from SpecElements::HTMLOptionElement

#option

Methods included from SpecElements::HTMLOptGroupElement

#optgroup

Methods included from SpecElements::HTMLDataListElement

#datalist

Methods included from SpecElements::HTMLSelectElement

#select

Methods included from SpecElements::HTMLButtonElement

#button

Methods included from SpecElements::HTMLInputElement

#input

Methods included from SpecElements::HTMLLabelElement

#label

Methods included from SpecElements::HTMLFormElement

#form

Methods included from SpecElements::HTMLTableCellElement

#td, #th

Methods included from SpecElements::HTMLTableRowElement

#tr

Methods included from SpecElements::HTMLTableSectionElement

#tbody, #tfoot, #thead

Methods included from SpecElements::HTMLTableColElement

#col, #colgroup

Methods included from SpecElements::HTMLTableCaptionElement

#caption

Methods included from SpecElements::HTMLTableElement

#table

Methods included from SpecElements::HTMLAreaElement

#area

Methods included from SpecElements::HTMLMapElement

#map

Methods included from SpecElements::HTMLTrackElement

#track

Methods included from SpecElements::HTMLAudioElement

#audio

Methods included from SpecElements::HTMLVideoElement

#video

Methods included from SpecElements::HTMLObjectElement

#object

Methods included from SpecElements::HTMLEmbedElement

#embed

Methods included from SpecElements::HTMLIFrameElement

#iframe

Methods included from SpecElements::HTMLImageElement

#img

Methods included from SpecElements::HTMLSourceElement

#source

Methods included from SpecElements::HTMLPictureElement

#picture

Methods included from SpecElements::HTMLModElement

#del, #ins

Methods included from SpecElements::HTMLBRElement

#br

Methods included from SpecElements::HTMLSpanElement

#span

Methods included from SpecElements::HTMLTimeElement

#time

Methods included from SpecElements::HTMLDataElement

#data

Methods included from SpecElements::HTMLAnchorElement

#a

Methods included from SpecElements::HTMLDivElement

#div

Methods included from SpecElements::HTMLDListElement

#dl

Methods included from SpecElements::HTMLLIElement

#li

Methods included from SpecElements::HTMLMenuElement

#menu

Methods included from SpecElements::HTMLUListElement

#ul

Methods included from SpecElements::HTMLOListElement

#ol

Methods included from SpecElements::HTMLQuoteElement

#blockquote, #q

Methods included from SpecElements::HTMLPreElement

#listing, #pre, #xmp

Methods included from SpecElements::HTMLHRElement

#hr

Methods included from SpecElements::HTMLParagraphElement

#p

Methods included from SpecElements::HTMLHeadingElement

#h1, #h2, #h3, #h4, #h5, #h6

Methods included from SpecElements::HTMLElement

#abbr, #acronym, #address, #article, #aside, #b, #basefont, #bdi, #bdo, #big, #center, #cite, #code, #dd, #dfn, #dt, #em, #figcaption, #figure, #footer, #header, #hgroup, #i, #kbd, #main, #mark, #menuitem, #nav, #nobr, #noembed, #noframes, #noscript, #plaintext, #rb, #rp, #rt, #rtc, #ruby, #s, #samp, #search, #section, #small, #strike, #strong, #sub, #summary, #sup, #tt, #u, #var, #wbr

Methods included from SpecElements::HTMLBodyElement

#body

Methods included from SpecElements::HTMLStyleElement

#style

Methods included from SpecElements::HTMLMetaElement

#meta

Methods included from SpecElements::HTMLLinkElement

#link

Methods included from SpecElements::HTMLBaseElement

#base

Methods included from SpecElements::HTMLTitleElement

#title

Methods included from SpecElements::HTMLHeadElement

#head

Methods included from SpecElements::HTMLHtmlElement

#html

Methods included from Writer

#initialize, #render

Class Method Details

.start {|obj| ... } ⇒ Object

To avoid subclassing a template, ‘start` can be used to yeild and return a Template.

html = FunHtml::Template.start do |t|
  t.doctype
  t.html do
    t.body { t.h1 "Body" }
  end
end

Yields:

  • (obj)


52
53
54
55
56
# File 'lib/fun_html/template.rb', line 52

def self.start(&block)
  obj = new
  yield obj if block
  obj
end

Instance Method Details

#attr(&blk) ⇒ Object

attr is short cut in the template to return a new Attribute

Template.start { |t| t.div(t.attr.id(‘my-div’), t.text ‘…’) }



96
97
98
99
100
101
102
# File 'lib/fun_html/template.rb', line 96

def attr(&blk)
  if blk
    Attribute.new(&blk)
  else
    Attribute.new
  end
end

#comment(comment_text = nil) ⇒ Object

generate a comment block



76
77
78
# File 'lib/fun_html/template.rb', line 76

def comment(comment_text = nil)
  write('<!--', '-->', nil, closing_char: '') { text comment_text.to_s }
end

#doctypeObject

generate the doctype



81
82
83
84
# File 'lib/fun_html/template.rb', line 81

def doctype
  @__buffer << '<!DOCTYPE html>'
  self
end

#join(templates) ⇒ Object

join an array of other templates into this template.



59
60
61
62
# File 'lib/fun_html/template.rb', line 59

def join(templates)
  templates.each { @__buffer << _1.render }
  self
end

#script(attributes = nil, &block) ⇒ Object

generate a script tag. The return the script code to the block. The code is not escaped.



88
89
90
91
# File 'lib/fun_html/template.rb', line 88

def script(attributes = nil, &block) # rubocop:disable Lint/UnusedMethodArgument
  body = yield
  write('<script', '</script>', attributes) { send :unsafe_text, body }
end

#text(value) ⇒ Object

text will generate the text node, this is the only way to insert strings into the template.

Template.start do |t|
  t.div { t.text  "Hello" }
end


70
71
72
73
# File 'lib/fun_html/template.rb', line 70

def text(value)
  @__buffer << ERB::Escape.html_escape(value)
  self
end