Dear Hanami,
I’m following the patterns established here by Paweł GitHub - katafrakt/palaver: a toy forum software in Hanami 2 to establish Phlex in my project. The two important pieces are this extension:
# frozen_string_literal: true
module Hanami
module Extensions
module Response
def render(view, **args)
context = UI::View::Context.new(self)
layout = UI::Layout.new(view, context, **args)
self.body = layout.call
end
end
end
end
Hanami::Action::Response.prepend(Hanami::Extensions::Response)
And this override:
# frozen_string_literal: true
require 'hanami'
module MyApp
# By default Hanami tries to find a matching class for current action,
# infer its name and instantiate it. However, this does not work with Phlex
# views. Hanami tries to create new instance with no arguments, but Phlex views
# use arguments in initialization. Therefore we need to disable this feature by
# creating a fake inferrer that always return empty array.
class FakeViewNameInferrer
def self.call(...) = []
end
class App < Hanami::App
config.actions.sessions = :cookie, { secret: ENV.fetch('SESSION_SECRET', nil) }
# disable default view inferrer
config.actions.view_name_inferrer = FakeViewNameInferrer
end
end
This all works but has two unfortunate consequences:
- I am no longer able to implicitly render view meaning I have to call:
response.render(Views::Home::Index)
in every action.
- More crucially I can no longer dynamically define a layout which is honestly something I need to do on a per slice basis.
Does anyone know how I might address either of these two issues?