Class: FunHtml::Template
- Inherits:
-
Object
- Object
- FunHtml::Template
- 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
-
.start {|obj| ... } ⇒ Object
To avoid subclassing a template,
startcan be used to yeild and return a Template.
Instance Method Summary collapse
-
#attr(&blk) ⇒ Object
attr is short cut in the template to return a new Attribute.
-
#comment(comment_text = nil) ⇒ Object
generate a comment block.
-
#doctype ⇒ Object
generate the doctype.
-
#join(templates) ⇒ Object
join an array of other templates into this template.
-
#script(attributes = nil, &block) ⇒ Object
generate a script tag.
-
#text(value) ⇒ Object
text will generate the text node, this is the only way to insert strings into the template.
Methods included from SpecElements::HTMLMarqueeElement
Methods included from SpecElements::HTMLFontElement
Methods included from SpecElements::HTMLParamElement
Methods included from SpecElements::HTMLFrameSetElement
Methods included from SpecElements::HTMLFrameElement
Methods included from SpecElements::HTMLDirectoryElement
Methods included from SpecElements::HTMLUnknownElement
#applet, #bgsound, #blink, #isindex, #keygen, #multicol, #nextid, #spacer
Methods included from SpecElements::HTMLCanvasElement
Methods included from SpecElements::HTMLSlotElement
Methods included from SpecElements::HTMLTemplateElement
Methods included from SpecElements::HTMLDialogElement
Methods included from SpecElements::HTMLDetailsElement
Methods included from SpecElements::HTMLLegendElement
Methods included from SpecElements::HTMLFieldSetElement
Methods included from SpecElements::HTMLMeterElement
Methods included from SpecElements::HTMLProgressElement
Methods included from SpecElements::HTMLOutputElement
Methods included from SpecElements::HTMLTextAreaElement
Methods included from SpecElements::HTMLOptionElement
Methods included from SpecElements::HTMLOptGroupElement
Methods included from SpecElements::HTMLDataListElement
Methods included from SpecElements::HTMLSelectElement
Methods included from SpecElements::HTMLButtonElement
Methods included from SpecElements::HTMLInputElement
Methods included from SpecElements::HTMLLabelElement
Methods included from SpecElements::HTMLFormElement
Methods included from SpecElements::HTMLTableCellElement
Methods included from SpecElements::HTMLTableRowElement
Methods included from SpecElements::HTMLTableSectionElement
Methods included from SpecElements::HTMLTableColElement
Methods included from SpecElements::HTMLTableCaptionElement
Methods included from SpecElements::HTMLTableElement
Methods included from SpecElements::HTMLAreaElement
Methods included from SpecElements::HTMLMapElement
Methods included from SpecElements::HTMLTrackElement
Methods included from SpecElements::HTMLAudioElement
Methods included from SpecElements::HTMLVideoElement
Methods included from SpecElements::HTMLObjectElement
Methods included from SpecElements::HTMLEmbedElement
Methods included from SpecElements::HTMLIFrameElement
Methods included from SpecElements::HTMLImageElement
Methods included from SpecElements::HTMLSourceElement
Methods included from SpecElements::HTMLPictureElement
Methods included from SpecElements::HTMLModElement
Methods included from SpecElements::HTMLBRElement
Methods included from SpecElements::HTMLSpanElement
Methods included from SpecElements::HTMLTimeElement
Methods included from SpecElements::HTMLDataElement
Methods included from SpecElements::HTMLAnchorElement
Methods included from SpecElements::HTMLDivElement
Methods included from SpecElements::HTMLDListElement
Methods included from SpecElements::HTMLLIElement
Methods included from SpecElements::HTMLMenuElement
Methods included from SpecElements::HTMLUListElement
Methods included from SpecElements::HTMLOListElement
Methods included from SpecElements::HTMLQuoteElement
Methods included from SpecElements::HTMLPreElement
Methods included from SpecElements::HTMLHRElement
Methods included from SpecElements::HTMLParagraphElement
Methods included from SpecElements::HTMLHeadingElement
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
Methods included from SpecElements::HTMLStyleElement
Methods included from SpecElements::HTMLMetaElement
Methods included from SpecElements::HTMLLinkElement
Methods included from SpecElements::HTMLBaseElement
Methods included from SpecElements::HTMLTitleElement
Methods included from SpecElements::HTMLHeadElement
Methods included from SpecElements::HTMLHtmlElement
Methods included from Writer
Class Method Details
.start {|obj| ... } ⇒ Object
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 |
#doctype ⇒ Object
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 |