Use the response method to define the UI of the page by using Matestack's HTML rendering or calling components.
classSomePage<Matestack::Ui::Pagedefresponse div id: "div-on-page"doSomeComponent.()endendend
classSomeComponent<Matestack::Ui::Componentdefresponse div id: "my-component"do plain "hello world!"endendend
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:
classSomePage<Matestack::Ui::Pagedefresponse div id: "my-page"do my_partial "foo from page"endendprivate# optionally mark your partials as privatedefmy_partialtext div class: "nested" 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" plain textendendend
Include the module in the page:
classSomePage<Matestack::Ui::PageincludeMySharedPartialsdefresponse div id: "my-page"do my_partial "foo from component"endendend
Helper methods
Not only can instance methods be used as "partials" but as general helpers performing any kind of logic or data resolving:
classSomePage<Matestack::Ui::Pagedefresponse div id: "my-page"doif is_admin? latest_users.each do|user| div do plain user.name # ...endendelse plain "not authorized"endendendprivate# optionally mark your helper methods as privatedefis_admin?true# some crazy Ruby logic!enddeflatest_usersUser.last(10) # calling ActiveRecord models for exampleendend
Prepare
Use a prepare method to resolve instance variables before rendering a page if required.
classSomePage<Matestack::Ui::Pagedefprepare @some_data ="some data"enddefresponse div id: "my-page"do plain @some_dataendendend
Params access
A page can access request information, e.g. url query params, by calling the params method:
classSomePage<Matestack::Ui::Pagedefresponse div id: "my-page"do plain params[:foo]endendend
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.
Passing data to pages
Sometimes you want to pass in data from the calling controller action into the page. This works the same way as seen at components:
classSoomeController<ActionController::BaseincludeMatestack::Ui::Core::Helperdefsome_page render SomePage, foo: 'bar', bar: 'baz'endend