class Tubby::Renderer
Constants
- SELF_CLOSING_TAGS
- TAGS
Public Class Methods
new(target)
click to toggle source
# File lib/tubby.rb, line 31 def initialize(target) @target = target end
Public Instance Methods
<<(obj)
click to toggle source
# File lib/tubby.rb, line 35 def <<(obj) obj = obj.to_tubby if obj.respond_to?(:to_tubby) if obj.is_a?(Tubby::Template) obj.render_with(self) elsif obj.respond_to?(:to_html) @target << obj.to_html elsif obj.respond_to?(:html_safe?) && obj.html_safe? @target << obj else @target << CGI.escape_html(obj.to_s) end self end
__attrs!(attrs)
click to toggle source
# File lib/tubby.rb, line 57 def __attrs!(attrs) if attrs.key?(:data) && attrs[:data].is_a?(Hash) # Flatten present `data: {k1: v1, k2: v2}` attribute, inserting `data-k1: v1, data-k2: v2` # into exact place where the attribute was attrs = Hash[attrs.flat_map do |key, value| if key == :data && value.is_a?(Hash) value.map { |k, v| [:"data-#{k}", v] } else [[key, value]] end end] end attrs.each do |key, value| if value.is_a?(Array) value = value.compact.join(" ") end if value key = key.to_s.tr("_", "-") if value == true @target << " #{key}" else value = CGI.escape_html(value.to_s) @target << " #{key}=\"#{value}\"" end end end end
doctype!()
click to toggle source
# File lib/tubby.rb, line 53 def doctype! @target << "<!DOCTYPE html>" end
raw!(text)
click to toggle source
# File lib/tubby.rb, line 49 def raw!(text) @target << text.to_s end
script(content = nil, **attrs)
click to toggle source
# File lib/tubby.rb, line 135 def script(content = nil, **attrs) @target << "<script" __attrs!(attrs) @target << ">" if content if content =~ /<(!--|script|\/script)/ raise "script tags can not contain #$&" end @target << content end @target << "</script>" end
self_closing_tag!(name, **attrs)
click to toggle source
# File lib/tubby.rb, line 97 def self_closing_tag!(name, **attrs) @target << "<" << name __attrs!(attrs) @target << ">" end
tag!(name, content = nil, **attrs) { || ... }
click to toggle source
# File lib/tubby.rb, line 88 def tag!(name, content = nil, **attrs) @target << "<" << name __attrs!(attrs) @target << ">" self << content if content yield if block_given? @target << "</" << name << ">" end