Call the Logic from the Interface

Understand the concept of calling the logic from the interface.

We'll cover the following

Public functions

We’ve seen that we can start a new game from within an IEx session begun with iex -S mix phx.server. This means we can call the IslandsEngine.Game module’s public functions from any Phoenix component.

In the beginning, we talked a lot about decoupling the interface from the business logic. We made a bold claim and said that this would make our work trivially easy. Throughout the chapter, we’ve shown how we can keep the two separated but included in a common project. What we haven’t shown yet is how these two will communicate. It’s time to back up that claim.

We’re going to walk through this pretty quickly. We work with a few new files but won’t spend a lot of time explaining them.

The good news is that Phoenix provides a full, working example of all the files we’ll need. It’s the welcome page we saw when we started the server for the first time. We’re going to modify some of those files to perform an experiment.

We create a form in the index template /islands_interface/islands_interface_web/templates/page/index.html.eex.

<div class="jumbotron">
  <h2><%= gettext "Welcome to %{name}", name: "Phoenix!" %></h2>
  <p class="lead">
    A productive web framework that does not compromise speed and
    <%= form_tag("/test") do
      [tag(:input, type: "text", name: "name"),
      tag(:input, type: "submit", value: "New Game")]

We use the form_tag/2 and tag/2 functions from the :phoenix_html Application. We saw :phoenix_html in our list of dependencies from mix.exs.

Get hands-on with 1200+ tech skills courses.