Files
something-erlang/lib/something_erlang_web.ex

114 lines
2.6 KiB
Elixir
Raw Permalink Normal View History

2022-05-23 15:57:15 +02:00
defmodule SomethingErlangWeb do
@moduledoc """
The entrypoint for defining your web interface, such
2023-01-18 16:13:51 +01:00
as controllers, components, channels, and so on.
2022-05-23 15:57:15 +02:00
This can be used in your application as:
use SomethingErlangWeb, :controller
2023-01-18 16:13:51 +01:00
use SomethingErlangWeb, :html
2022-05-23 15:57:15 +02:00
2023-01-18 16:13:51 +01:00
The definitions below will be executed for every controller,
component, etc, so keep them short and clean, focused
2022-05-23 15:57:15 +02:00
on imports, uses and aliases.
Do NOT define functions inside the quoted expressions
2023-01-18 16:13:51 +01:00
below. Instead, define additional modules and import
those modules here.
2022-05-23 15:57:15 +02:00
"""
2023-01-18 16:13:51 +01:00
def static_paths, do: ~w(assets fonts images favicon.ico robots.txt)
def router do
2022-05-23 15:57:15 +02:00
quote do
2023-01-18 16:13:51 +01:00
use Phoenix.Router, helpers: false
2022-05-23 15:57:15 +02:00
2023-01-18 16:13:51 +01:00
# Import common connection and controller functions to use in pipelines
2022-05-23 15:57:15 +02:00
import Plug.Conn
2023-01-18 16:13:51 +01:00
import Phoenix.Controller
import Phoenix.LiveView.Router
end
end
def channel do
quote do
use Phoenix.Channel
2022-05-23 15:57:15 +02:00
end
end
2023-01-18 16:13:51 +01:00
def controller do
2022-05-23 15:57:15 +02:00
quote do
2023-01-18 16:13:51 +01:00
use Phoenix.Controller,
formats: [:html, :json],
layouts: [html: SomethingErlangWeb.Layouts]
2022-05-23 15:57:15 +02:00
2023-01-18 16:13:51 +01:00
import Plug.Conn
import SomethingErlangWeb.Gettext
2022-05-23 15:57:15 +02:00
2023-01-18 16:13:51 +01:00
unquote(verified_routes())
2022-05-23 15:57:15 +02:00
end
end
def live_view do
quote do
use Phoenix.LiveView,
2023-01-18 16:13:51 +01:00
layout: {SomethingErlangWeb.Layouts, :app}
2022-05-23 15:57:15 +02:00
2023-01-18 16:13:51 +01:00
unquote(html_helpers())
2022-05-23 15:57:15 +02:00
end
end
def live_component do
quote do
use Phoenix.LiveComponent
2023-01-18 16:13:51 +01:00
unquote(html_helpers())
2022-05-23 15:57:15 +02:00
end
end
2023-01-18 16:13:51 +01:00
def html do
2022-05-23 15:57:15 +02:00
quote do
use Phoenix.Component
2023-01-18 16:13:51 +01:00
# Import convenience functions from controllers
import Phoenix.Controller,
only: [get_csrf_token: 0, view_module: 1, view_template: 1]
2022-05-23 15:57:15 +02:00
2023-01-18 16:13:51 +01:00
# Include general helpers for rendering HTML
unquote(html_helpers())
2022-05-23 15:57:15 +02:00
end
end
2023-01-18 16:13:51 +01:00
defp html_helpers do
2022-05-23 15:57:15 +02:00
quote do
2023-01-18 16:13:51 +01:00
# HTML escaping functionality
import Phoenix.HTML
# Core UI components and translation
import SomethingErlangWeb.CoreComponents
2022-05-23 15:57:15 +02:00
import SomethingErlangWeb.Gettext
2023-01-18 16:13:51 +01:00
# Shortcut for generating JS commands
alias Phoenix.LiveView.JS
# Routes generation with the ~p sigil
unquote(verified_routes())
2022-05-23 15:57:15 +02:00
end
end
2023-01-18 16:13:51 +01:00
def verified_routes do
2022-05-23 15:57:15 +02:00
quote do
2023-01-18 16:13:51 +01:00
use Phoenix.VerifiedRoutes,
endpoint: SomethingErlangWeb.Endpoint,
router: SomethingErlangWeb.Router,
statics: SomethingErlangWeb.static_paths()
2022-05-23 15:57:15 +02:00
end
end
@doc """
2024-06-02 13:31:19 +02:00
When used, dispatch to the appropriate controller/live_view/etc.
2022-05-23 15:57:15 +02:00
"""
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
end