Project

General

Profile

[ask] passing variable to view hook?

Added by rangga rahadian over 8 years ago

how to passing a variable from class hook to hook html?
i tried write simple code as below, but the variable didnt printed.

hook file :

  module Hooks
     class showUserHook < Redmine::Hook::ViewListener
       @var = 1 + 2
        render_on :view_account_left_bottom, :partial => "account/showhook" 
     end
  end

and views/account/showhook.html.erb file:

  <html>
    user id = <%= @user.id %>
    var value = <%= @var %>
  </html>

output :

user id = 5
var value =

expected output :

user id = 5
var value = 3

thanks for your reply


Replies (1)

RE: [ask] passing variable to view hook? - Added by Akiko Takano over 8 years ago

Hi. I think you can pass variables into partials as local variables.
How about?

module Hooks
     class showUserHook < Redmine::Hook::ViewListener
       var = 1 + 2
       render_on :view_account_left_bottom, partial: "account/showhook”,  locals: { var: var }
     end
end

Such as account/_showhook.html.erb

  <html>
    user id = <%= @user.id %>
    var value = <%= var %>
  </html>

Note: Instance variable like user user is passed from controller.
I hope this can help you.

    (1-1/1)