I'm having trouble understanding how to implement the Presenter pattern alongside MVC using Sinatra.
I've read lots of posts and none really are focused on the specific use cases i'm interested in.
Just to be clear I'm already using the MVC pattern as defined in "Sinatra Up and Running" (see: https://github.com/Integralist/sinatra-mvc/ -> my code has changed since I originally created this repo but it'll give you an idea of the MVC structure I'm using).
what I'm trying to achieve...
My understanding of the Presenter is to collate Model data and then pass it through to a View. Then pass that back to the Controller who then renders the View.
The reason I want to use the Presenter pattern is to not only keep the Controllers clean but to also be able to compose a page made up of 'components' (chunks of HTML) so I can reuse some components on different pages.
how to implement?
But I'm not sure how to implement this. Can some one please show me an example of how I could do this (pseudo-code is fine if writing a real example would be a lot of work)?
Here's some example code of how I envision it could look but I'm not sure if what I'm thinking would be correct or of how the implementation would work (I've tried different things but hit issues with my lack of understanding of how Sinatra works under the covers)...
class ContactController < ApplicationController
get '/' do
@component_a = require './presenter/a'
@component_b = require './presenter/b'
erb :contact
end
end
...I assume I'd still need some basic logic in my View to determine where about in the page to load the component, like...
<%
if @component_a
erb :"#{@component_a}"
end
%>
So this makes me think that loading a presenter should maybe return something that allows that to happen.
Any suggestions on how to do this better is also very welcome.
Thanks!