Overview
HTML Rendering
Matestack’s rendering mechanism takes care of converting Ruby into HTML:
will be rendered to:
As you can see, Ruby method calls like
div
, img
... are mapped to simple HTML tags. matestack-ui-core
support all kinds of standard HTML tags enabling you to build a well known DOM structure while writing and utilizing pure Ruby!
As you can see, you can add CSS classes and ids as well as custom tag attributes. This way matestack-ui-core
can be combined with various CSS frameworks or your custom styles. It’s already fun to write pure Ruby instead of HTML or any other templating engine syntax but this approach is really paying of, when you start using Ruby's language features in order to split your UI implementation into various small chunks, organized through included modules, class inheritance or simply multiple Ruby methods within one class!
The above shown Ruby code lives in Ruby classes inheriting from Matestack::Ui::Component
, Matestack::Ui::Page
or Matestack::Ui::App
They are described in the next section:
Basic UI Building Blocks
Depending on your desired Rails integration mode (see below), you might only need a subset of the now presented building blocks
Matestack’s basic UI building blocks are called apps, pages and components:
An app can be compared with a Rails layout, a page can be compared with a Rails view and a component can be best compared with a Rails partial.
Apps, pages and components are Ruby classes, implementing a response
method which will then define specific parts of the UI using pure Ruby methods. We will see in a bit how this looks like.
Components
Components use Matestack's HTML rendering mechanism in a response
method and may additionally call other components in order to define a specific UI. Components can be used on apps, pages and other components. Additionally they can be used on Rails views (see below)
On the above shown example, a reusable card component was created, which can be used across the whole application taking multiple required or optional options. A component may also take blocks or named slots for flexible markup injection.
Components can be called directly like Components::Card.(title: "foo", body: "bar")
which will return the desired HTML string. If desired, you can create alias methods in order to avoid the class call syntax:
which then allows you to call the card component like card(title: "foo", body: "bar")
if the above shown module is included properly.
Learn more about components:
ComponentsPages
As said, a Matestack page can be compared to a Rails view and might be yielded within a layout provided by an associated Matestack app (see below). The page itself uses Matestack's HTML rendering mechanism in a response
method and may additionally call other components in order to define a specific UI.
In this basic example the page is using the Ruby methods div
, span
and plain
in order to create the desired UI and call the above defined component Components::Card
Pages are used as Rails view substitutes and therefore called in a Rails controller action:
Learn more about Pages:
PagesApps
An app uses components in order to define the layout of your application. It might implement a header and a footer for example. Just like a Rails layout would yield a Rails view, an app yields a page. The app uses Matestack's HTML rendering mechanism in a response
method and may additionally call other components in order to define a specific UI.
In this basic example the app is using the methods h1
and main
in order to create the markup as well as a yield
in order to yield a page on a specific position.
Usually an app implies a specific context of your application. Multiple pages are then scoped within that context, which could lead to a file structure like:
and then used in a controller like this:
See below (Full Matestack) for a more concrete example!
Learn more about Apps:
AppsRails Integration Modes
There are several ways to use the presented building blocks in your Rails app. matestack-ui-core
is designed to be progressively integrated into existing Rails apps and views. Leveraging the full power and beauty is best done when going full Matestack though!
Matestack components on Rails views
→ Only components are used here and there
Full Matestack
→ Apps, pages and components used together as a Rails view layer substitute
Matestack components on Rails views
If you already have plenty of Rails views (ERB, Haml or Slim) and want to start creating small UI components in pure Ruby, you are able to use components on these existing views.
The class is then called on your Rails view, in this case an ERB view:
This approach is suitable for existing apps and a good idea to migrate to Matestack step by step. If you start with a blank Rails app, we recommend to go full Matestack right away**!**
Full Matestack
Going full Matestack means, using pages, components and apps as a (scoped) complete substitute for the Rails view layer. We're not using Rails views anymore but Matestack pages instead. This behavior is mainly managed within Rails controllers.
It's totally valid to serve multiple scopes within one Rails app. Think of a web shop Rails app, consisting of a client-facing storefront UI and a backoffice admin UI. You're free to use full Matestack on one scope and a completely different view layer on the other.
Let’s review the classic Rails request/response cycle:
A request is coming in and Rails routing is calling the specified Rails controller action. Nothing new here. Within the Rails action we’re now telling the controller not to render a Rails view wrapped in a Rails layout but instead render a Matestack page wrapped in a Matestack app. All other controller based business logic stays untouched. This is why gems like devise or pundit for example can be used in harmony with matestack-ui-core
Thinking of the describe example of a Webshop backoffice admin UI, the implementation with dynamic page transitions may look like this:
In a file structure like:
Last updated