Roda::HashBranchViewNamespace
roda-hash_branch_view_namespace is a Roda plugin that automatically appends subdirectories to your view template search path (append_view_subdir) matching your hash branch names and namespaces when routing requests.
It builds upon Roda's built-in hash_branches and view_options plugins to streamline organizing template views according to your hash branch namespace hierarchy.
Installation
Add this line to your application's Gemfile:
gem "roda-hash_branch_view_namespace"And then execute:
bundle installOr install it directly:
gem install roda-hash_branch_view_namespaceUsage
Enable the plugin in your Roda application:
class App < Roda
plugin :render, views: "views"
plugin :hash_branch_view_namespace
route do |r|
r.hash_branches
r.on "admin" do
r.hash_branches :admin
end
end
endAutomatic View Subdirectory Appending
When you define a hash branch with hash_branch, the plugin automatically calls append_view_subdir("#{namespace}/#{segment}") before executing the branch block.
Default Namespace
# Views will automatically look into views/users/
hash_branch("users") do |r|
# Calls append_view_subdir("users")
view("index") # renders views/users/index.erb
endCustom Namespace
# Views will automatically look into views/admin/users/
hash_branch(:admin, "users") do |r|
# Calls append_view_subdir("admin/users")
view("index") # renders views/admin/users/index.erb
endHow It Works
- Loads the
hash_branchesandview_optionsplugins automatically. - Wraps route definitions created via
hash_branch(namespace, segment)to automatically executeappend_view_subdirprior to yielding control to your route block. - Fully supports route redefinition, route removal, subclassing/inheritance, and app freezing (
App.freeze).
Development
After checking out the repository, run:
bin/setupTo run the test suite:
bundle exec rake specFor an interactive prompt to experiment:
bin/consoleContributing
Bug reports and pull requests are welcome on GitHub at https://github.com/roda-project/roda-hash_branch_view_namespace.
License
The gem is available as open source under the terms of the MIT License.