How to pass a variable to a layout?

前端 未结 5 2077
时光说笑
时光说笑 2021-02-05 14:27

I have a two versions of my application layout, which are differs only in a few lines. Consider following example:

!!!    
%html
    %head
        # a lot of cod         


        
5条回答
  •  既然无缘
    2021-02-05 15:09

    There is two another option to do, what actually the OP asked:

    #1

    in your layout:

    - if flag ||= false
      # variant 1
    - else
      # variant 2
    

    in your application controller (this is the trick):

    layout 'application' # or whatever
    

    in any kind of controller:

    render :locals => { :flag => true }
    

    My guess would be, the layout processing is happening later cause of the "dynamic" (not really) layout definition and that generates the necessary methods for all the keys inside of local_assigns. So maybe the instance variable is a performanter solution. Any thoughts about that? Please leave a comment.

    #2

    You could just use the local_assigns variable like:

    - if local_assigns[:flag] ||= false
      # variant 1
    - else
      # variant 2
    

    and then in any of your controller:

    render :locals => { :flag => true }
    

提交回复
热议问题