Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jamesmartin/unconditional-rails-views
Example using some syntactic sugar to move existential conditionals from view templates to helpers
https://github.com/jamesmartin/unconditional-rails-views
Last synced: 17 days ago
JSON representation
Example using some syntactic sugar to move existential conditionals from view templates to helpers
- Host: GitHub
- URL: https://github.com/jamesmartin/unconditional-rails-views
- Owner: jamesmartin
- Created: 2015-02-21T10:37:59.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-02-22T22:53:37.000Z (almost 10 years ago)
- Last Synced: 2024-10-27T04:49:34.541Z (about 2 months ago)
- Language: Ruby
- Size: 156 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Unconditional Rails Views
This is a simple Rails app with one controller, one view template and a couple
of partials. The app demonstrates an experiment to move existential conditional
logic out of view templates and partials, leaving behind declarative method
calls to indicate the dependencies of that view.Here's a before and after:
```haml
%h1 Greetings
= render partial: 'shared/greeting', locals: { name: 'Bill Evans' }
``````haml
- if defined?(name)
%p Hello, #{name}.
- else
%p Hello, there.- if defined?(name)
Listen, #{name}, it's been fun, but I have to get going.- if @foo.present?
%p Here's something from the controller: #{foo}
``````haml
- depend_on(local_assigns)
- with_local(:name) do |name|
%p Hello, #{name}.
- without_local(:name) do
%p Hello, there.- with_local(:name) do |name|
%p Listen, #{name}, it's been fun, but I have to get going.- with_global(:foo) do |foo|
%p Here's something from the controller: #{foo}
```This might not look like much, syntactically, but it represents a [large shift](https://github.com/jamesmartin/unconditional-rails-views/blob/master/lib/unconditional_view/helpers.rb) in
*responsibility* from the perspective of the template or partial.There are more [interesting
examples](https://github.com/jamesmartin/unconditional-rails-views/blob/master/app/views/application/index.html.haml), using a special interface to allow the
calling template control the visibility of the local based on some runtime
behaviour.