A layout class defines a layout within its response method and is suppose to yield the content of a page.
Response
Use the response method to define the UI of the app by using Matestack's HTML rendering and optionally calling components.
classSomeApp<Matestack::Ui::Appdefresponse nav do a path: some_rails_path, text: "Navigate!"end main doyieldend footer do div id: "div-on-app"doSomeComponent.()endendendend
Partials and helper methods
Use partials to keep the code dry and indentation layers manageable!
Local partials on page level
In the page definition, see how this time from inside the response, the my_partial method below is called:
classSomeApp<Matestack::Ui::Appdefresponse nav do a path: some_rails_path, text: "Navigate!"end main doyieldend footer do div id: "div-on-app"doSomeComponent.()end my_partial "foo from app"endendprivate# optionally mark your partials as privatedefmy_partialtext div class: "nested"do plain textendendend
Partials defined in modules
Extract code snippets to modules for an even better project structure. First, create a module:
moduleMySharedPartialsdefmy_partialtext div class: "nested"do plain textendendend
Include the module in the page:
classSomeApp<Matestack::Ui::AppincludeMySharedPartialsdefresponse nav do a path: some_rails_path, text: "Navigate!"end main doyieldend footer do div id: "div-on-app"doSomeComponent.()end my_partial "foo from app"endendend
Helper methods
Not only can instance methods be used as "partials" but as general helpers performing any kind of logic or data resolving:
classSomeApp<Matestack::Ui::Appdefresponse nav do a path: some_rails_path, text: "Navigate!"end main doif is_admin?yieldelse plain "not authorized"endendendprivate# optionally mark your helper methods as privatedefis_admin?true# some crazy Ruby logic!endend
Prepare
Use a prepare method to resolve instance variables before rendering a page if required.
classSomeApp<Matestack::Ui::Appdefprepare @heading ="Foo"enddefresponse h1 @heading nav do a path: some_rails_path, text: "Navigate!"end main doyieldendendend
Params access
An app can access request information, e.g. url query params, by calling the params method:
classSomeApp<Matestack::Ui::Appdefresponse nav do a path: some_rails_path, text: "Navigate!"end main do plain params[:foo] # "bar" yieldendendend
Now, visiting the respective route to the page, e.g. via /xyz?foo=bar, the page reads the [:foo] from the params and displays it.