Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inline templates #309

Open
saturnflyer opened this issue Jul 21, 2015 · 6 comments
Open

inline templates #309

saturnflyer opened this issue Jul 21, 2015 · 6 comments

Comments

@saturnflyer
Copy link

I'm kicking the tires of cells and am using a cell to handle the display of an address. I've found that the view object is useful for handling display logic, but the use of an extra template file seems like a lot for the small case I have.
Here's an example cell. It has some simple methods in it so that if I only have 1 value out of a typical pair, it'll only display the one value and won't have hanging commas with something like , New Netherland or Franklin,

class AddressCell < Cell::ViewModel
  def show
    render
  end

  private

  property :address1
  property :address2
  property :address3
  property :state
  property :city
  property :province
  property :country
  property :postal

  def city_state
    [city, state_name].reject{|att| att.blank? }.join(', ')
  end

  def state_name
    (state.presence && state.name) || province
  end

  def country_and_postal
    [country_name, postal].reject{|att| att.blank? }.join(', ')
  end

  def country_name
    country.presence && country.name
  end
end

The template is as small as this:

<%= address1 %><br />
<%= address2 %><br />
<%= address3 %><br />
<%= city_state %><br />
<%= country_and_postal %>

As I was hooking this up, I wondered about just using the model part of it and not the template file:

<%= cell :address, location do %>
  <%= address1 %><br />
  <%= address2 %><br />
  <%= address3 %><br />
  <%= city_state %><br />
  <%= country_and_postal %>
<%- end -%>

I'm not sure that my app would stay this way, but it would allow me to play with handling the display logic and adjusting the layout before deciding to place it into a template file.

I started digging into the code but before trying to figure it out, I wanted both a sanity check and to see if this is something you'd be interested in having in the project.

@selectport
Copy link

+1 ... i'm finding the similar approach in react.js for simple 'templates' is really nice ... having it along with the normal cell template files would be a nice option to add ....

@apotonick
Copy link
Member

Wow, I love this "using the model part" idea - I never thought of this! It shouldn't be hard to implement. We could simply catch the block and evaluate it in cells context.

Many people use content_tag blocks in cell methods to achieve just that.

I had a slightly different idea a few years ago and I was evaluating either Erector or Ambre where you have the templates defined in Ruby, in the cell itself instead of a rather clumsy template snippet. This does not only speed up things but also allows way better overriding semantics, where you can programmatically overwrite parts of your "view" in subclasses.

class SongCell < Cell::ViewModel
  view do
    div do
      address1
      address2
    end
  end

I remember I loved the Erector gem by @alexch but we never integrated the two projects. Maybe it's time now? 🎆

@fernandes
Copy link
Member

for reference erector and arbre

Thats a fuckin' amazing idea... specially because we can "overwrite" and customize views (manually or programmatically) using ruby OOP

trying to go one step further on @saturnflyer idea, what is we could create "wrappers" for the view?

so we could do:

<%= cell :address, location do %>
  <%= address1 %><br />
  <%= address2 %><br />
  <%= address3 %><br />
  <%= city_state %><br />
  <%= country_and_postal %>
<%- end -%>

or

cell :address, location, wrapper: :bootstrap

this would be a great addition to "form-builder-sprite-thing" no @apotonick ?

@alexch
Copy link

alexch commented Jul 22, 2015

Another Erector spinoff is https://github.com/ageweke/fortitude which is a
bit cleaner codebase and more up to date with the latest Rails. I've been
using it on a client project and (apart for some headaches with nested
blocks passed in to Rails helpers) it's working fine.

On Wed, Jul 22, 2015 at 8:40 AM, Celso Fernandes [email protected]
wrote:

for reference erector https://github.com/erector/erector and arbre
https://github.com/activeadmin/arbre

Thats a fuckin' amazing idea... specially because we can "overwrite" and
customize views (manually or programmatically) using ruby OOP

trying to go one step further on @saturnflyer
https://github.com/saturnflyer idea, what is we could create "wrappers"
for the view?

so we could do:

<%= cell :address, location do %> <%= address1 %>

<%= address2 %>

<%= address3 %>

<%= city_state %>

<%= country_and_postal %><%- end -%>

or

cell :address, location, wrapper: :bootstrap

this would be a great addition to "form-builder-sprite-thing" no
@apotonick https://github.com/apotonick ?


Reply to this email directly or view it on GitHub
#309 (comment).

Alex Chaffee - [email protected]
http://alexchaffee.com
http://twitter.com/alexch

@saturnflyer
Copy link
Author

I attempted to implement this but don't understand enough about cells structure to do it quickly.
So I looked to see if I could just implement it in my cell with this:

  def show(&block)
    if block
      instance_eval(&block)
    else
      render
    end
  end

But it seems like the block is ignored.

Related to the template builders, I'd rather bake in easy support but not the actual implementation. There are already 3 options (Erector, Arbre, and Fortitude) mentioned. Why not take the approach that cells does with rails helpers and just force users to add what they need?

@alexch
Copy link

alexch commented Jul 22, 2015 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants