Display view from another view

I have several views that I would like to appear on every view that displays, but without duplicating code or violating specifications, I cannot find a way to accomplish this.

Here is my current code, dialing calls in all views:

def ImOnABoat :: Views
  def layout
    html do
      head do title "Petstore" end
      body do yield end
    end
  end

  def navigation
    p "Welcome to our tiny petstore!"
  end

  def poodle
    navigation # Have to duplicate this in every view
    p "We have a poodle!"
  end

  def fluffy_bunny
    navigation # Have to duplicate this in every view
    p "Come see-- OH CRAP IT A VELOCIRAPTOR!"
  end
end

, , , , .

def layout
  def head do title "Petstore" end
  nav  # This is not inside <body>!
  def body do yield end
end
+3
2

, Ruby, navigation :

module ImOnABoat::Views
  def layout
    html do
      head { title "Petstore" }
      body do
        navigation
        yield
      end
    end
  end

  def navigation
    p "Welcome to our tiny petstore!"
  end

  def poodle
    p "We have a poodle!"
  end

  def fluffy_bunny
    p "Come see-- OH CRAP IT A VELOCIRAPTOR!"
  end
end
+2

Source: https://habr.com/ru/post/1735773/


All Articles