A template is a file that describes the body of a response. It is rendered by bounding the context of a view and using a template engine.

Naming

For simplicity sake, there is a correlation between the view class name and the template file name. It’s the translation of the name into a path: from Dashboard::Index to dashboard/index.

The remaining part is made of multiple file extensions. The first is relative to the format and the latter is for the template engine.

For a given view named Web::Views::Dashboard::Index, there must be at least one template dashboard/index.[format].[engine] under the templates directory.

Nested Templates

To render a partial in other template call render method with partial option:

  1. # Given a partial under:
  2. # templates/shared/_sidebar.html.erb
  3. #
  4. # In the layout template:
  5. # templates/application.html.erb
  6. #
  7. <%= render partial: 'shared/sidebar' %>

To render a template in other template call render method with template option:

  1. # Given a template under:
  2. # templates/articles/index.html.erb
  3. #
  4. # In the layout template:
  5. # templates/application.html.erb
  6. #
  7. <%= render template: 'articles/index' %>

Custom Template

If we want to associate a different template to a view, we can use template.

  1. # apps/web/views/dashboard/index.rb
  2. module Web::Views::Dashboard
  3. class Index
  4. include Web::View
  5. template 'home/index'
  6. end
  7. end

Our view will look for apps/web/templates/home/index.* template.

Engines

Hanami looks at the last extension of a template file name to decide which engine to use (eg index.html.erb will use ERb). The builtin rendering engine is ERb, but Hanami supports countless rendering engines out of the box.

This is a list of the supported engines. They are listed in order of higher precedence, for a given extension. For instance, if ERubis is loaded, it will be preferred over ERb to render .erb templates.

EngineExtensions
Erubiserb, rhtml, erubis
ERberb, rhtml
Redcarpetmarkdown, mkd, md
RDiscountmarkdown, mkd, md
Kramdownmarkdown, mkd, md
Marukumarkdown, mkd, md
BlueClothmarkdown, mkd, md
Asciidoctorad, adoc, asciidoc
Builderbuilder
CSVrcsv
CoffeeScriptcoffee
WikiClothwiki, mediawiki, mw
Creolewiki, creole
Etannietn, etanni
Hamlhaml
Lessless
Liquidliquid
Markabymab
Nokogirinokogiri
Plainhtml
RDocrdoc
Radiusradius
RedClothtextile
Sasssass
Scssscss
Slimslim
Stringstr
Yajlyajl

In order to use a different template engine we need to bundle the gem and to use the right file extension.

  1. # app/web/templates/dashboard/index.html.haml
  2. %h1 Dashboard

Directory

Templates are located in the default directory templates, located under an application’s directory apps/web. If we want to customize this location, we can amend our application’s configuration.

  1. # apps/web/application.rb
  2. module Web
  3. class Application < Hanami::Application
  4. configure do
  5. # ...
  6. templates 'path/to/templates'
  7. end
  8. end
  9. end

The application will now look for templates under apps/web/path/to/templates.