haml-rails

repository·master·Indexed 21 days ago

https://github.com/haml/haml-rails

Integrates the Haml templating engine into Rails applications. It automates engine configuration, sets the default template format to HTML5, and overrides standard Rails generators (controller, mailer, scaffold, and authentication) to produce Haml templates instead of ERB. The library includes tools to convert existing ERB layouts and views to Haml via the haml:application_layout generator and the haml:erb2haml Rake task.

Tokens
1.7K
Snippets
10
Records
14
Agent score
76%

What's inside haml-rails

  1. Install haml-rails

    master

    To use Haml as your templating engine in a Rails application, add the haml-rails gem to your Gemfile. This automatically configures Rails to use Haml for generated resources, views, and mailers, and ensures Haml templates are respected by the view template cache digestor.

    gem "haml-rails"
  2. Convert the Rails application layout to Haml

    master

    You can convert your existing ERB application layout (typically app/views/layouts/application.html.erb) to Haml using the haml:application_layout generator.

    Prerequisite: This command requires the html2haml gem. You must install it locally before running the generator:

    gem install html2haml

    Steps:

    1. Run the conversion command: rails generate haml:application_layout convert
    2. After successful conversion, manually delete the original app/views/layouts/application.html.erb file to ensure Rails uses the new .haml version.
    $ rails generate haml:application_layout convert
  3. Convert all .erb views to .haml format

    master

    To bulk-convert all .erb views in your application to .haml, use the rails haml:erb2haml command.

    Behavior:

    • If .haml files already exist for certain .erb files, the task will prompt you to either replace them or leave them in place.
    • After conversion, the task will ask if you want to delete the original .erb files.

    Automation: If you are running this in a script and want to avoid interactive prompts, use the HAML_RAILS_DELETE_ERB environment variable:

    • HAML_RAILS_DELETE_ERB=true: Deletes the original .erb files automatically.
    • HAML_RAILS_DELETE_ERB=false: Keeps the .erb files and skips the prompt.
    $ rails haml:erb2haml
    
    # To automate deletion of ERB files:
    $ HAML_RAILS_DELETE_ERB=true rails haml:erb2haml
  4. Enable source annotations in Haml templates

    master

    For Rails versions 4.2 and newer, haml-rails enables source annotations. This allows you to use special comments in your Haml files that Rails can track to provide better error reporting and source mapping.

    To use source annotations, follow the pattern of using a comment tag that the extension recognizes. The extension is configured to look for the following pattern:

    -# tag: message
  5. Configure Haml as the default template engine in Rails

    master
    When haml-rails is loaded, it automatically configures the Rails application generators to use :haml as the default template engine. This ensures that when you run generators (like rails generate controller), the resulting view files use the .haml extension instead of .erb.
  6. Use Haml-rails for scaffold generation

    master

    When haml-rails is installed in a Rails application, it overrides the default Rails scaffold generator to use Haml templates instead of ERB. This allows you to generate resource controllers, models, and views (index, edit, show, new, and the _form partial) directly in .html.haml format.

    The generator supports standard Rails scaffold arguments (field:type) and respects the current Rails version to select appropriate template structures (e.g., handling Rails 7+ specific partial patterns).

    rails generate scaffold User name:string email:string
  7. Convert application layout from ERB to Haml

    master

    The ApplicationLayoutGenerator is a Rails generator designed to automate the conversion of your Rails application's main layout file from ERB to Haml.

    It performs the following steps:

    1. Locates the existing app/views/layouts/application.html.erb file.
    2. Uses the html2haml command-line tool to convert the content.
    3. Creates a new app/views/layouts/application.html.haml file.

    Requirements:

    • The html2haml executable must be available in your system's PATH.

    Note: After a successful conversion, you must manually remove the original ERB file (app/views/layouts/application.html.erb) to prevent Rails from potentially using the wrong template.

    # Run the generator via the Rails CLI
    rails generate haml:application_layout
  8. Set default Haml template format to HTML5

    master
    The haml-rails integration automatically sets the global Haml::Template.options[:format] to :html5 during the Rails initialization process. This ensures that all Haml templates rendered within the Rails application default to HTML5 output.
  9. Compatibility with older Rails versions

    master

    The current version of haml-rails requires Rails 5.1 or later. If you are working on a legacy application, use the following version constraints in your Gemfile:

    • Rails 4: Use haml-rails version ~> 1.0.0.
    • Rails 3: Use haml-rails version ~> 0.4.0.
    # For Rails 4
    gem "haml-rails", "~> 1.0.0"
    
    # For Rails 3
    gem "haml-rails", "~> 0.4.0"
  10. Generate authentication templates using Haml

    master

    The Haml::Generators::AuthenticationGenerator is a Rails generator that creates authentication-related view templates using Haml instead of ERB. It generates the following files:

    • app/views/passwords/new.html.haml
    • app/views/passwords/edit.html.haml
    • app/views/sessions/new.html.haml

    This generator inherits from the standard Rails Erb::Generators::AuthenticationGenerator, meaning it is intended to be used within a Rails application context where authentication scaffolding is being generated.

    # Usage within a Rails environment
    rails generate haml:authentication