3425b08a2f
* Create .rspec * Update .rubocop.yml * Update .travis.yml * Update hanami-helpers.gemspec * Update Rakefile * Create escape_helper_spec.rb * Create escape_helper_spec.rb * Create escape_helper_spec.rb * Delete escape_helper_spec.rb * Create fixtures.rb * Create version_spec.rb * Update escape_helper_spec.rb * Create html_helper_spec.rb * create form_helper_spec in progress * Create link_to_helper_spec.rb * Create routing_helper_spec.rb * Create number_formatting_helper_spec.rb * Update escape_helper_spec.rb * Create html_helper_spec * Create number_formatter_helper_spec * Create routing_helper_spec * Rename number_formatter_helper_spec to number_formatter_helper_spec.rb * Rename html_helper_spec to html_helper_spec.rb * Rename routing_helper_spec to routing_helper_spec.rb * Create form_helper_spec.rb * Create link_to_helper_spec.rb * Update .travis.yml * Update fixtures.rb * Create new.html.erb * Add files via upload * Create spec_helper.rb * Create html_builder_spec.rb * Update html_helper_spec.rb * Update escape_helper_spec.rb * Update form_helper_spec.rb * Update html_helper_spec.rb * Update escape_helper_spec.rb * Update version_spec.rb * Update * Update form_helper_spec.rb * Update html_helper_spec.rb * Update number_formatting_helper_spec.rb * clear trailing whitespace * Update spec_helper.rb * Update html_helper_spec.rb * Update hanami-helpers.gemspec * Update escape_helper_spec.rb * fix indentation issues * Update hanami-helpers.gemspec * Update Rakefile * Update form_helper_spec.rb * Update form_helper_spec.rb * Fix to skip errors about array and indent * Fix to clear CI issue This should fix this issue: test/fixtures.rb:58:5: W: Lint/AmbiguousBlockAssociation: Parenthesize the param html to make sure that the block will be associated with the + method call. (https://github.com/bbatsov/ruby-style-guide#syntax) html { div 'Hello' } + html { div 'Hanami' } ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * Update form_helper_spec.rb * Update fixtures.rb * Update fixtures.rb * Update form_helper_spec.rb * Update fixtures.rb |
||
---|---|---|
lib | ||
spec | ||
test | ||
.gitignore | ||
.rspec | ||
.rubocop.yml | ||
.travis.yml | ||
.yardopts | ||
CHANGELOG.md | ||
Gemfile | ||
LICENSE.md | ||
README.md | ||
Rakefile | ||
hanami-helpers.gemspec |
README.md
Hanami::Helpers
View helpers for Ruby applications
Status
Contact
- Home page: http://hanamirb.org
- Mailing List: http://hanamirb.org/mailing-list
- API Doc: http://rdoc.info/gems/hanami-helpers
- Bugs/Issues: https://github.com/hanami/helpers/issues
- Support: http://stackoverflow.com/questions/tagged/hanami
- Chat: https//chat.hanamirb.org
Rubies
Hanami::Helpers supports Ruby (MRI) 2.3+ and JRuby 9.1.5.0+
Installation
Add this line to your application's Gemfile:
gem 'hanami-helpers'
And then execute:
$ bundle
Or install it yourself as:
$ gem install hanami-helpers
Usage
Hanami::Helpers
offers a set of utilities to enrich web views.
HTML helper
HTML5 markup generator (#html
).
View:
module Users
class Show
include Hanami::Helpers
def sidebar
html.aside(id: 'sidebar') do
p "Languages", class: 'title'
ul do
li "Italian"
li "English"
end
end
end
end
end
Template:
<%= sidebar %>
Output:
<aside id="sidebar">
<p class="title">Languages</p>
<ul>
<li>Italian</li>
<li>English</li>
</ul>
</aside>
Form Helper
Form generator for HTML5 (#form_for
)
Template Usage
Template:
<%=
form_for :book, routes.books_path do
text_field :title
submit 'Create'
end
%>
Output:
<form action="/books" method="POST" accept-charset="utf-8" id="book-form">
<input type="text" name="book[title]" id="book-id" value="">
<button type="submit">Create</button>
</form>
View Usage
View:
module Books
class New
include Hanami::Helpers
def form
form_for :book, routes.books_path do
text_field :title
submit 'Create'
end
end
end
end
Template:
<%= form %>
Output:
<form action="/books" method="POST" accept-charset="utf-8" id="book-form">
<input type="text" name="book[title]" id="book-id" value="">
<button type="submit">Create</button>
</form>
Reuse Code
Views:
module Books
class New
include Hanami::Helpers
def form
Form.new(:book, routes.books_path)
end
def submit_label
'Create'
end
end
class Edit
include Hanami::Helpers
def form
Form.new(:book, routes.book_path(id: book.id), {book: book}, {method: :patch})
end
def submit_label
'Update'
end
end
end
Templates:
# books/new.html.erb
<%= render partial: 'books/form' %>
# books/edit.html.erb
<%= render partial: 'books/form' %>
# books/_form.html.erb
<%=
form_for form, class: 'form-horizontal' do
text_field :title
submit submit_label
end
%>
Output for new:
<form action="/books" method="POST" accept-charset="utf-8" id="book-form">
<input type="text" name="book[title]" id="book-id" value="">
<button type="submit">Create</button>
</form>
Output for edit:
<form action="/books/23" method="POST" accept-charset="utf-8" id="book-form">
<input type="hidden" name="_method" value="PATCH">
<input type="text" name="book[title]" id="book-id" value="TDD">
<button type="submit">Update</button>
</form>
Escape helper
HTML (#h
), HTML attribute (#ha
) and URL (#hu
) escape helpers.
View:
module Users
class Show
include Hanami::Helpers
def home_page_link
%(<a href="#{ hu(user.home_page_url) }" title="#{ ha(user.name} }'s website">#{ h(user.website_name) }</a>)
end
def code_snippet
raw user.code_snippet
end
end
end
Template:
<%= home_page_link %>
<%= code_snippet %>
Output:
<a href="https://example.org" title="Maria's website">My Blog</a>
<code>puts "Hello, World!"</code>
Routing Helper
Hanami and Hanami::Router integration (#routes
).
View:
module Home
class Index
include Hanami::Helpers
def link_to_home
%(<a href="#{ routes.home_path }">Home</a>)
end
end
end
Template:
<%= link_to_home %>
Output:
<a href="/">Home</a>
Number Formatting Helper
Format numbers (#format_number
).
View:
module Home
class Index
include Hanami::Helpers
def visitors_count
format_number '1000'
end
end
end
Template:
<p><%= visitors_count %></p>
Output:
<p>1,000</p>
Philosophy
All the Hanami helpers are modules to include.
Most of the time they inject private methods. This restriction prevents helper methods to be used on the outside (eg. in a template).
We want to encourage developers to use meaningful and simple APIs in their templates.
Bad style example
module Users
class Show
include Hanami::Helpers
end
end
<%= format_number user.followers_count %>
This style increases the complexity of the template and it makes testing hard.
Good style example
module Users
class Show
include Hanami::Helpers
def followers_count
format_number user.followers_count
end
end
end
<%= followers_count %>
This simplifies the markup.
In order to test the value that will be printed becomes easier: Users::Show#followers_count
.
Versioning
Hanami::Helpers uses Semantic Versioning 2.0.0
Contributing
- Fork it ( https://github.com/hanami/helpers/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Copyright
Copyright © 2014-2017 Luca Guidi – Released under MIT License
This project was formerly known as Lotus (lotus-helpers
).