API

Response

Use the response method to define the UI of the page by using Matestack's HTML rendering or calling components.
1
class SomePage < Matestack::Ui::Page
2
3
def response
4
div id: "div-on-page" do
5
SomeComponent.()
6
end
7
end
8
9
end
Copied!
1
class SomeComponent < Matestack::Ui::Component
2
3
def response
4
div id: "my-component" do
5
plain "hello world!"
6
end
7
end
8
9
end
Copied!

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:
1
class SomePage < Matestack::Ui::Page
2
3
def response
4
div id: "my-page" do
5
my_partial "foo from page"
6
end
7
end
8
9
private # optionally mark your partials as private
10
11
def my_partial text
12
div class: "nested"
13
plain text
14
end
15
end
16
17
end
Copied!

Partials defined in modules

Extract code snippets to modules for an even better project structure. First, create a module:
1
module MySharedPartials
2
3
def my_partial text
4
div class: "nested"
5
plain text
6
end
7
end
8
9
end
Copied!
Include the module in the page:
1
class SomePage < Matestack::Ui::Page
2
3
include MySharedPartials
4
5
def response
6
div id: "my-page" do
7
my_partial "foo from component"
8
end
9
end
10
11
end
Copied!

Helper methods

Not only can instance methods be used as "partials" but as general helpers performing any kind of logic or data resolving:
1
class SomePage < Matestack::Ui::Page
2
3
def response
4
div id: "my-page" do
5
if is_admin?
6
latest_users.each do |user|
7
div do
8
plain user.name # ...
9
end
10
end
11
else
12
plain "not authorized"
13
end
14
end
15
end
16
17
private # optionally mark your helper methods as private
18
19
def is_admin?
20
true # some crazy Ruby logic!
21
end
22
23
def latest_users
24
User.last(10) # calling ActiveRecord models for example
25
end
26
27
end
Copied!

Prepare

Use a prepare method to resolve instance variables before rendering a page if required.
1
class SomePage < Matestack::Ui::Page
2
3
def prepare
4
@some_data = "some data"
5
end
6
7
def response
8
div id: "my-page" do
9
plain @some_data
10
end
11
end
12
13
end
Copied!

Params access

A page can access request information, e.g. url query params, by calling the params method:
1
class SomePage < Matestack::Ui::Page
2
3
def response
4
div id: "my-page" do
5
plain params[:foo]
6
end
7
end
8
9
end
Copied!
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:
1
class SoomeController < ActionController::Base
2
3
include Matestack::Ui::Core::Helper
4
5
def some_page
6
render SomePage, foo: 'bar', bar: 'baz'
7
end
8
9
end
Copied!
1
class SomePage < Matestack::Ui::Page
2
3
required :foo
4
optional :bar
5
6
def response
7
div id: "my-page" do
8
plain context.foo # "bar"
9
plain context.bar # "baz"
10
end
11
end
12
13
end
Copied!