App API
An app defines a layout within its response method and yields the content of a page in its layout.

Response

Use the response method to define the UI of the app by using Matestack's HTML rendering and optionally calling components.
1
class SomeApp < Matestack::Ui::App
2
3
def response
4
nav do
5
a path: some_rails_path, text: "Navigate!"
6
end
7
main do
8
yield
9
end
10
footer do
11
div id: "div-on-app" do
12
SomeComponent.()
13
end
14
end
15
end
16
17
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 SomeApp < Matestack::Ui::App
2
3
def response
4
nav do
5
a path: some_rails_path, text: "Navigate!"
6
end
7
main do
8
yield
9
end
10
footer do
11
div id: "div-on-app" do
12
SomeComponent.()
13
end
14
my_partial "foo from app"
15
end
16
end
17
18
private # optionally mark your partials as private
19
20
def my_partial text
21
div class: "nested" do
22
plain text
23
end
24
end
25
26
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" do
5
plain text
6
end
7
end
8
9
end
Copied!
Include the module in the page:
1
class SomeApp < Matestack::Ui::App
2
3
include MySharedPartials
4
5
def response
6
nav do
7
a path: some_rails_path, text: "Navigate!"
8
end
9
main do
10
yield
11
end
12
footer do
13
div id: "div-on-app" do
14
SomeComponent.()
15
end
16
my_partial "foo from app"
17
end
18
end
19
20
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 SomeApp < Matestack::Ui::App
2
3
def response
4
nav do
5
a path: some_rails_path, text: "Navigate!"
6
end
7
main do
8
if is_admin?
9
yield
10
else
11
plain "not authorized"
12
end
13
end
14
end
15
16
private # optionally mark your helper methods as private
17
18
def is_admin?
19
true # some crazy Ruby logic!
20
end
21
22
end
Copied!

Prepare

Use a prepare method to resolve instance variables before rendering a page if required.
1
class SomeApp < Matestack::Ui::App
2
3
def prepare
4
@heading = "Foo"
5
end
6
7
def response
8
h1 @heading
9
nav do
10
a path: some_rails_path, text: "Navigate!"
11
end
12
main do
13
yield
14
end
15
end
16
17
end
Copied!

Params access

An app can access request information, e.g. url query params, by calling the params method:
1
class SomeApp < Matestack::Ui::App
2
3
def response
4
nav do
5
a path: some_rails_path, text: "Navigate!"
6
end
7
main do
8
plain params[:foo] # "bar"
9
yield
10
end
11
end
12
13
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.
Last modified 7mo ago