This helper makes available an HTML5 generator that is template engine independent. It’s a private method for views and layouts called #html.

Usage

This is how it will look used with a layout:

  1. module Web
  2. module Views
  3. class ApplicationLayout
  4. include Web::Layout
  5. def sidebar
  6. html.aside(id: 'sidebar') do
  7. div 'hello'
  8. end
  9. end
  10. end
  11. end
  12. end
  1. <%= sidebar %>

It generates:

  1. <aside id="sidebar">
  2. <div>hello</div>
  3. </aside>

Features

  • It knows how to close tags according to HTML5 spec (1)
  • It accepts content as first argument (2)
  • It accepts builder as first argument (3)
  • It accepts content as block which returns a string (4)
  • It accepts content as a block with nested markup builders (5)
  • It builds attributes from given hash (6)
  • it combines attributes and block (7)
  1. # 1
  2. html.div # => <div></div>
  3. html.img # => <img>
  4. # 2
  5. html.div('hello') # => <div>hello</div>
  6. # 3
  7. html.div(html.p('hello')) # => <div><p>hello</p></div>
  8. # 4
  9. html.div { 'hello' }
  10. # =>
  11. #<div>
  12. # hello
  13. #</div>
  14. # 5
  15. html.div do
  16. p 'hello'
  17. end
  18. # =>
  19. #<div>
  20. # <p>hello</p>
  21. #</div>
  22. # 6
  23. html.div('hello', id: 'el', 'data-x': 'y') # => <div id="el" data-x="y">hello</div>
  24. # 7
  25. html.div(id: 'yay') { 'hello' }
  26. # =>
  27. #<div id="yay">
  28. # hello
  29. #</div>

It supports complex markup constructs, without the need of concatenate tags. In the following example, there are two div tags that we don’t need link together.

  1. html.section(id: 'container') do
  2. div(id: 'main') do
  3. p 'Main content'
  4. end
  5. div do
  6. ul(id: 'languages') do
  7. li 'Italian'
  8. li 'English'
  9. end
  10. end
  11. end
  12. # =>
  13. # <section id="container">
  14. # <div id="main">
  15. # <p>Main Content</p>
  16. # </div>
  17. #
  18. # <div>
  19. # <ul id="languages">
  20. # <li>Italian</li>
  21. # <li>English</li>
  22. # </ul>
  23. # </div>
  24. # </section>

The result is a very clean Ruby API.

Custom tags

Hanami helpers support 100+ most common tags, such as div, video or canvas. However, HTML5 is fast moving target so we wanted to provide an open interface to define new or custom tags.

The API is really simple: #tag must be used for a self-closing tag, where #empty_tag does the opposite.

  1. html.tag(:custom, 'Foo', id: 'next') # => <custom id="next">Foo</custom>
  2. html.empty_tag(:xr, id: 'next') # => <xr id="next">

The link_to helper, while appearing similar to tags generated by html, has different semantics and will not appear as part of the generator, as one might expect.

Since the return value of link_to is a string, it must be treated as such when combined with the generator offered by html.

Alone in a tag, the return value of link_to can be used as the content for the tag.

  1. html.div do
  2. link_to('hello', routes.root_path, class: 'btn')
  3. end
  4. # => <div>
  5. # => <a href="/" class="btn">hello</a>
  6. # => </div>

Multiple link_to can be concatted together, which returns a string, and then acts as the content of the tag.

  1. html.div do
  2. link_to('Users', routes.users_path, class: 'btn') +
  3. link_to('Books', routes.books_path, class: 'btn')
  4. end

If you require multiple link_to inside the same tag, and there are other tags inside that tag, the return value of link_to will need to be passed to one of the html generator helpers.

This might be a block or inline tag such as div or span, but if no wrapping element is desired, the text helper can be used to render the link as-is.

  1. html.div do
  2. text link_to('Users', routes.users_path, class: 'btn')
  3. hr
  4. text link_to('Books', routes.books_path, class: 'btn')
  5. end
  6. # => <div>
  7. # => <a href="/users" class="btn">Users</a>
  8. # => <hr>
  9. # => <a href="/posts" class="btn">Books</a>
  10. # => </div>

Auto escape

The tag contents are automatically escaped for security reasons:

  1. html.div('hello') # => <div>hello</div>
  2. html.div { 'hello' } # => <div>hello</div>
  3. html.div(html.p('hello')) # => <div><p>hello</p></div>
  4. html.div do
  5. p 'hello'
  6. end # => <div><p>hello</p></div>
  7. html.div("<script>alert('xss')</script>")
  8. # => "<div>&lt;script&gt;alert(&apos;xss&apos;)&lt;&#x2F;script&gt;</div>"
  9. html.div { "<script>alert('xss')</script>" }
  10. # => "<div>&lt;script&gt;alert(&apos;xss&apos;)&lt;&#x2F;script&gt;</div>"
  11. html.div(html.p("<script>alert('xss')</script>"))
  12. # => "<div><p>&lt;script&gt;alert(&apos;xss&apos;)&lt;&#x2F;script&gt;</p></div>"
  13. html.div do
  14. p "<script>alert('xss')</script>"
  15. end
  16. # => "<div><p>&lt;script&gt;alert(&apos;xss&apos;)&lt;&#x2F;script&gt;</p></div>"

HTML attributes aren’t automatically escaped, in case we need to use a value that comes from a user input, we suggest to use #ha, which is the escape helper designed for this case. See Escape Helpers for a deep explanation.

View Context

Local variables from views are available inside the nested blocks of HTML builder:

  1. module Web
  2. module Views
  3. module Books
  4. class Show
  5. include Web::View
  6. def title_widget
  7. html.div do
  8. h1 book.title
  9. end
  10. end
  11. end
  12. end
  13. end
  14. end
  1. <div id="content">
  2. <%= title_widget %>
  3. </div>
  1. <div id="content">
  2. <div>
  3. <h1>The Work of Art in the Age of Mechanical Reproduction</h1>
  4. </div>
  5. </div>