Module: Phlex::Elements
- Included in:
- HTML, HTML::StandardElements, HTML::VoidElements, SVG::StandardElements
- Defined in:
- lib/phlex/elements.rb
Overview
Extending this module provides the #register_element macro for registering your own custom elements. It's already extended by HTML and SVG.
Instance Method Summary collapse
-
#register_element(method_name, tag: nil) ⇒ Symbol
Register a custom element.
Instance Method Details
#register_element(method_name, tag: nil) ⇒ Symbol
35 36 37 38 39 40 41 42 43 44 45 46 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 |
# File 'lib/phlex/elements.rb', line 35 def register_element(method_name, tag: nil) tag ||= method_name.name.tr("_", "-") class_eval(<<-RUBY, __FILE__, __LINE__ + 1) # frozen_string_literal: true def #{method_name}(**attributes, &block) target = @_context.target if attributes.length > 0 # with attributes if block # with content block target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">" yield_content(&block) target << "</#{tag}>" else # without content block target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << "></#{tag}>" end else # without attributes if block # with content block target << "<#{tag}>" yield_content(&block) target << "</#{tag}>" else # without content block target << "<#{tag}></#{tag}>" end end #{'flush' if tag == 'head'} nil end alias_method :_#{method_name}, :#{method_name} RUBY registered_elements[method_name] = tag method_name end |