Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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.