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

ActionView:Template turning locals Hash into an array #19204

Closed
anthonydahanne opened this issue Mar 5, 2015 · 6 comments
Closed

ActionView:Template turning locals Hash into an array #19204

anthonydahanne opened this issue Mar 5, 2015 · 6 comments

Comments

@anthonydahanne
Copy link

Hello,
I've been trying (with Rails 4.1.9) to register and use a template handler :

module MustacheTemplateHandler
  def self.call(template)
    if template.locals.include? :mustache
      "Mustache.render(#{template.source.inspect}, mustache).html_safe"
    else
      "#{template.source.inspect}.html_safe"
    end
  end
end
ActionView::Template.register_template_handler(:mustache, MustacheTemplateHandler)

and then I've been trying to use it, from an erb view, in particular with a Hash as a template local :

<%= render "project", :mustache => "{'planet':'world'}" %>

well, guess what, template.locals turns out to be an array with a single element being the string "mustache" - no trace of the rest of the Hash (:mustache => "{'planet':'world'}")
It seems like the locals array was turned into an array of strings (the strings being the hashes key) between the view and my template handler, I have no clue how it happened,
Do you have any idea why ? How should I use my template handler with views variables ?
thanks a lot in advance !

@amaierhofer
Copy link
Contributor

Template#locals only contains a string array of keys passed.

The locals hash you are looking for is not present at compile time of the template. The decision to render mustache or not must be inlined in the ruby code returned from your handler.

module MustacheTemplateHandler
  def self.call(template)
    <<-RUBY
    if local_assigns.key?(:mustache)
      Mustache.render(#{template.source.inspect}, mustache).html_safe
    else
      #{template.source.inspect}.html_safe
    end
    RUBY
  end
end

Also mustache seems needs a hash to work on, so you should probably render your template like

<%= render "project", :mustache => { :greeting => 'hi there' }  %>

and reference the value in your template like so

<p>says {{greeting}}</h1>

Also you should be careful with using html_safe here.

@rafaelfranca
Copy link
Member

Please don't use the mailing list to ask question.

@kreba
Copy link

kreba commented Mar 18, 2015

Dear @rafaelfranca, I highly respect your many contributions to Rails. However, this was rude.
The issue had been open for two weeks without anyone caring, then when someone finally steps in and helps the guy, you join the stage and slap everyone in the face. Not very nice.
Especially since a member of the Rails Core Team had suggested amaierhofer and me to look into this issue.

@rafaelfranca
Copy link
Member

Sorry, that was an automatic comment.

I'm very happy that the question was answered and I also do agree I could
thank you guys for doing so, next time I'll try to do.

Although I agree I could say a lot of nice words, this issue is still a
question, and our contributing guides and contributing.md files say that we
don't accept questions in the issue tracker so I could not see why keep it
open.

Anyway, thank you much for looking this issue, and also for the protest, I
did a mistake sorry 🙏

On Wed, Mar 18, 2015, 19:46 Raffael Krebs notifications@github.com wrote:

Dear @rafaelfranca https://github.com/rafaelfranca, I highly respect
your many contributions to Rails. However, this was rude.
The issue had been open for two weeks without anyone caring, then when
someone finally steps in and helps the guy, you join the stage and slap
everyone in the face. Not very nice.
Especially since a member of the Rails Core Team had suggested amaierhofer
and me to look into this issue.


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

@kreba
Copy link

kreba commented Mar 19, 2015

Aaaah, thanks for your nice words! ;)
Educating people on how to use the issue tracker must be tedious work at times. Thank you for taking care of that! ❤️

@anthonydahanne
Copy link
Author

hello all
Thanks for your answer @amaierhofer - that makes sense, I guess debugging the rendering can't be done effectively during compile time of the template ! (I was observing the values during a debugging session)
Sorry about not asking the question in the rails talk mailing list first - at the time I thought it might be a bug.
But yeah I should have asked first on the ML.
Thanks again all for your time

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

4 participants