leshill / handlebars_assets

Use handlebars.js templates with the Rails asset pipeline.
MIT License
649 stars 159 forks source link

Accessing View Helpers From Templates #140

Open PeterMozesMerl opened 8 years ago

PeterMozesMerl commented 8 years ago

There is an issue (#89) with the same name. It has been closed with the argument that "View Helpers generally access non-constant data."

This is true for certain helpers. There are others:

2.2.1 :024 > puts image_tag '/{{source}}', alt: '{{alt}}'
<img alt="{{alt}}" src="/{{source}}" />
 => nil 
2.2.1 :025 > puts text_field_tag :title, '{{json_value}}'
<input type="text" name="title" id="title" value="{{json_value}}" />
 => nil 
2.2.1 :026 > puts check_box_tag :sure, '{{value}}', false, id: '{{is-sure}}'
<input type="checkbox" name="sure" id="{{is-sure}}" value="{{value}}" />
 => nil 

Sure, certain options won’t work, like setting the "checked" with JSON and without jQuery. Still, having the Rails helpers would be a huge gain. I think most of the people who use this gem would be able to figure out what works and what doesn’t.

PeterMozesMerl commented 8 years ago

Also:

= simple_form_for User.new(first_name: '{{first_name}}', last_name: '{{last_name}}') do |f|
    = f.input :first_name
    = f.input :last_name
<form novalidate="novalidate" class="simple_form new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
  <input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="XRvodW7AhzpZRMgzYfQazRP3XzNU5MCFzAke2UmQHbfHotfFmCLqlV4zlk7cYbwiChgGx8g8/ZJ1ZNO6p40W3w==" />
  <div class="form-group string optional user_first_name">
    <label class="string optional control-label" for="user_first_name">First name</label>
    <input class="string optional form-control" type="text" value="{{first_name}}" name="user[first_name]" id="user_first_name" />
  </div>
  <div class="form-group string optional user_last_name">
    <label class="string optional control-label" for="user_last_name">Last name</label>
    <input class="string optional form-control" type="text" value="{{last_name}}" name="user[last_name]" id="user_last_name" />
  </div>
</form>

This would work fine in slimbars. The only thing that would require jQuery / whatever is the authenticity_token.

AlexRiedler commented 8 years ago

I am willing to accept PR for this, I just don't have the time right now to develop it :disappointed:

PeterMozesMerl commented 8 years ago

Fair enough. I should learn more about the internals of Rails anyway.