Skip to content

Instantly share code, notes, and snippets.

@yamitake
Created July 11, 2025 07:07
Show Gist options
  • Select an option

  • Save yamitake/74c070ef1da90929d23e92f7aa688dcd to your computer and use it in GitHub Desktop.

Select an option

Save yamitake/74c070ef1da90929d23e92f7aa688dcd to your computer and use it in GitHub Desktop.
Lightweight HTML tag DSL for Rails views, including support for both standard and self-closing tags.
module HtmlTagHelper
NORMAL_TAGS = %i[
a abbr address article aside audio b bdi bdo blockquote body button canvas caption
cite code colgroup data datalist dd del details dfn dialog div dl dt em fieldset figcaption figure
footer form h1 h2 h3 h4 h5 h6 header hgroup html i iframe ins kbd label legend li main map mark menu
meter nav noscript object ol optgroup option output p picture pre progress q rp rt ruby s samp script
section select small span strong style sub summary sup table tbody td template textarea tfoot th thead
time title tr u ul var video
]
VOID_TAGS = %i[
area base br col embed hr img input link meta source track wbr
]
def self.included(base)
# 通常の開閉タグ用ヘルパー
NORMAL_TAGS.each do |tag|
base.define_method(tag) do |content = nil, **options, &block|
content_tag(tag, content || capture(&block), **options)
end
end
# 自閉タグ用ヘルパー
VOID_TAGS.each do |tag|
base.define_method(tag) do |**options|
tag(tag, **options)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment