emcee icon indicating copy to clipboard operation
emcee copied to clipboard

Issue Using Erb Templating in HTML Imports

Open chrants opened this issue 10 years ago • 9 comments

When I attempt to erb in an HTML import I get an error. For example, I have this in assets/components/post.html.erb:

<polymer-element name="form-post">
  <template>
    <%= form_tag("/posts", method: 'post') do %>
      <%= text_area_tag "post[body]" %>
      <%= submit_tag %>
    <% end %>
  </template>
  <script>
    Polymer('form-post', {
      ready: function() {
        //...
      }
    });
  </script>
</polymer-element>

When I attempt to load a page I get syntax error, unexpected ')'. I have even attempted to do a blank form_tag with no parameters:

<polymer-element name="form-post">
  <template>
    <%= form_tag %>
  </template>
  <script>
    Polymer('form-post', {
      ready: function() {
        //...
      }
    });
  </script>
</polymer-element>

in which case I get undefined method 'form_tag'.

I'm not sure how erb-ing would work in templated HTML anyway. I was attempting to try it out. If erb-ing and HTML templating does not work in conjunction then that limits the capabilities of what you could do with frameworks like Polymer. Is there another way of doing this (besides stupid _helpers.html.erb, which doesn't really componentize -- it just renders things once)?

chrants avatar Jul 13 '14 22:07 chrants

Are you using one of the release versions of Emcee (such as v1.0.3), or are you pulling directly from the repository?

In either case, processing components with erb is something we can look at adding.

ahuth avatar Jul 13 '14 23:07 ahuth

It's the latest released gem (I didn't pull from github).

On Sun, Jul 13, 2014 at 6:11 PM, Andrew [email protected] wrote:

Are you using one of the release versions of Emcee (such as v1.0.3), or are you pulling directly from the repository?

In either case, processing components with erb is something we can look at adding.

— Reply to this email directly or view it on GitHub https://github.com/ahuth/emcee/issues/17#issuecomment-48854674.

chrants avatar Jul 14 '14 15:07 chrants

This is a rough idea, but perhaps a helper such as polymer_element could be added so you could do something like

<%= polymer_element "my-tag" do |e| %>
  <%= e.template_tag do |t| %>
    <%= t.form_for @something do |f| %>
      <%# Contents for @something %>
    <% end %>
  <% end %>
<% end %>

where the erb code inside of the template_tag block is evaluated whenever you call the tag. Perhaps calling the polymer_element tag block -- perhaps a web component-generator-agnostic name like element -- generates a helper method that allows you to then call my-tag in erb code.

Again, this is in no way highly thought out -- just listing ideas.

chrants avatar Jul 15 '14 22:07 chrants

I'm all about listing ideas!

There has to be a way to process a file with erb, too. I'm just not super familiar with how erb files (whether they be views or css/js) get access to all the methods they need.

Never been a better time to learn, though!

ahuth avatar Jul 15 '14 23:07 ahuth

@chriswins2much, Sorry for the delay, but I've finally started looking into this.

Right now, erb templating ~~doesn't~~ does work, as long as you don't use any of the view helpers. For example, <p><%= 2 + 2 %></p> will correctly yield <p>4</p>.

The problem is that the currently the components being processed don't have access to the view helpers such as form_tag.

I definitely want to get this working, but I'm not sure how to do it. This gem does exactly what we need, but how it does it is pretty complicated. Hopefully there's an easier way.

ahuth avatar Jul 23 '14 14:07 ahuth

Regarding your idea for a polymer_element tag: I really like that idea. I'm thinking it'll be better to split that out to a seperate gem, though. For now, I want to keep emcee more generic and not tied to one library or framework.

ahuth avatar Jul 23 '14 14:07 ahuth

hi, basically you would just need to make sure the binding that gets passed to the erb renderer is in the context of action view. or whatever context is required when you were doing this during the sprockets precompile step if you were using erb inside of a js or css asset.

it has been a while since i've looked into these internals so i could be referring to them incorrectly. I'm happy to take a crack at this though.

datapimp avatar Jul 24 '14 08:07 datapimp

@datapimp, I'm honestly a little bit lost about how to do this, but we definitely need it. If you could take a crack at it, I'd appreciate it!

ahuth avatar Jul 24 '14 19:07 ahuth

Hi, just wanted to update you guys. This is an issue that I believe should be fixed, but I just don't have the need or motivation to work on this gem anymore. Sorry!

I will accept pull requests, though.

ahuth avatar Dec 28 '14 17:12 ahuth