Skip to content

Commit

Permalink
Upgrade to functional views (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
JannikStreek authored Sep 26, 2024
1 parent 88f7f2d commit 353d253
Show file tree
Hide file tree
Showing 50 changed files with 1,580 additions and 1,024 deletions.
3 changes: 1 addition & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ config :mindwendel,
# Configures the endpoint
config :mindwendel, MindwendelWeb.Endpoint,
render_errors: [
view: MindwendelWeb.ErrorView,
accepts: ~w(html json),
formats: [html: MindwendelWeb.ErrorHTML, json: MindwendelWeb.ErrorJSON],
layout: false
],
url: [
Expand Down
93 changes: 54 additions & 39 deletions lib/mindwendel_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,80 +17,95 @@ defmodule MindwendelWeb do
and import those modules here.
"""

def static_paths, do: ~w(assets fonts images favicon.ico robots.txt)

def router do
quote do
use Phoenix.Router, helpers: false

# Import common connection and controller functions to use in pipelines
import Plug.Conn
import Phoenix.Controller
import Phoenix.LiveView.Router
end
end

def channel do
quote do
use Phoenix.Channel
end
end

def controller do
quote do
use Phoenix.Controller, namespace: MindwendelWeb
use Phoenix.Controller,
formats: [:html, :json],
layouts: [html: MindwendelWeb.Layouts]

import Plug.Conn
import MindwendelWeb.Gettext
alias MindwendelWeb.Router.Helpers, as: Routes

unquote(verified_routes())
end
end

def view do
def html do
quote do
use Phoenix.View,
root: "lib/mindwendel_web/templates",
namespace: MindwendelWeb
use Phoenix.Component
import Phoenix.HTML.Form

# Import convenience functions from controllers
import Phoenix.Controller,
only: [view_module: 1, view_template: 1]
only: [get_csrf_token: 0, view_module: 1, view_template: 1]

# Include shared imports and aliases for views
unquote(view_helpers())
# Include general helpers for rendering HTML
unquote(html_helpers())
end
end

def live_view do
defp html_helpers do
quote do
use Phoenix.LiveView,
layout: {MindwendelWeb.LayoutView, :live}
# HTML escaping functionality
import Phoenix.HTML
# Core UI components and translation
import MindwendelWeb.CoreComponents
import MindwendelWeb.Gettext

# Shortcut for generating JS commands
alias Phoenix.LiveView.JS

unquote(view_helpers())
# Routes generation with the ~p sigil
unquote(verified_routes())
end
end

def live_component do
def verified_routes do
quote do
use Phoenix.LiveComponent

unquote(view_helpers())
use Phoenix.VerifiedRoutes,
endpoint: MindwendelWeb.Endpoint,
router: MindwendelWeb.Router,
statics: MindwendelWeb.static_paths()
end
end

def router do
def live_view do
quote do
use Phoenix.Router
use Phoenix.LiveView,
layout: {MindwendelWeb.Layouts, :app}

import Plug.Conn
import Phoenix.Controller
import Phoenix.LiveView.Router
end
end
import MindwendelWeb.LiveHelpers

def channel do
quote do
use Phoenix.Channel
import MindwendelWeb.Gettext
unquote(html_helpers())
end
end

defp view_helpers do
def live_component do
quote do
# Use all HTML functionality (forms, tags, etc)
use Phoenix.HTML
use Phoenix.LiveComponent

# Import LiveView helpers (live_render, live_component, live_patch, etc)
import Phoenix.Component
import MindwendelWeb.LiveHelpers

# Import basic rendering functionality (render, render_layout, etc)
import Phoenix.View

import MindwendelWeb.ErrorHelpers
import MindwendelWeb.Gettext
alias MindwendelWeb.Router.Helpers, as: Routes
unquote(html_helpers())
end
end

Expand Down
Loading

0 comments on commit 353d253

Please sign in to comment.