Class: Phlex::HTML

Inherits:
SGML
  • Object
show all
Extended by:
SGML::Elements
Includes:
StandardElements, VoidElements
Defined in:
lib/phlex/html.rb

Defined Under Namespace

Modules: StandardElements, VoidElements

Constant Summary

Constants inherited from SGML

SGML::ERBCompiler, SGML::REF_ATTRIBUTES, SGML::UNSAFE_ATTRIBUTES

Instance Method Summary collapse

Methods included from SGML::Elements

__register_void_element__, __registered_elements__, register_element

Methods inherited from SGML

#cache, #call, call, #capture, #comment, #context, erb, #flush, #fragment, #internal_call, #json_escape, #low_level_cache, new, #plain, #raw, #render, #rendering?, #safe, #to_proc, #view_template, #whitespace

Instance Method Details

#content_typeObject

Returns the string “text/html”



37
38
39
# File 'lib/phlex/html.rb', line 37

def content_type
	"text/html"
end

#doctypeObject

Output an HTML doctype.



11
12
13
14
15
16
17
# File 'lib/phlex/html.rb', line 11

def doctype
	state = @_state
	return unless state.should_render?

	state.buffer << "<!doctype html>"
	nil
end

#filenameObject

Override to provide a filename for the HTML file



32
33
34
# File 'lib/phlex/html.rb', line 32

def filename
	nil
end

#svgObject



23
24
25
26
27
28
29
# File 'lib/phlex/html.rb', line 23

def svg(*, **, &)
	if block_given?
		super { render Phlex::SVG.new(&) }
	else
		super
	end
end

#tag(name, **attributes) ⇒ Object

Output an HTML tag dynamically, e.g:

“‘ruby tag(@tag_name, class: “title”) “`



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/phlex/html.rb', line 47

def tag(name, **attributes, &)
	state = @_state
	block_given = block_given?
	buffer = state.buffer

	unless state.should_render?
		yield(self) if block_given
		return nil
	end

	unless Symbol === name
		raise Phlex::ArgumentError.new("Expected the tag name to be a Symbol.")
	end

	if (tag = StandardElements.__registered_elements__[name]) || (tag = name.name.tr("_", "-")).include?("-")
		if attributes.length > 0 # with attributes
			if block_given # with content block
				buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << ">"
				if tag == "svg"
					render Phlex::SVG.new(&)
				else
					__yield_content__(&)
				end
				buffer << "</#{tag}>"
			else # without content
				buffer << "<#{tag}" << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << "></#{tag}>"
			end
		else # without attributes
			if block_given # with content block
				buffer << ("<#{tag}>")
				if tag == "svg"
					render Phlex::SVG.new(&)
				else
					__yield_content__(&)
				end
				buffer << "</#{tag}>"
			else # without content
				buffer << "<#{tag}></#{tag}>"
			end
		end
	elsif (tag = VoidElements.__registered_elements__[name])
		if block_given
			raise Phlex::ArgumentError.new("Void elements cannot have content blocks.")
		end

		if attributes.length > 0 # with attributes
			buffer << "<#{tag}" << (::Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << ">"
		else # without attributes
			buffer << "<#{tag}>"
		end

		nil
	else
		raise Phlex::ArgumentError.new("Invalid HTML tag: #{name}")
	end
end