1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web do
7 A module that keeps using definitions for controllers,
10 This can be used in your application as:
12 use Pleroma.Web, :controller
13 use Pleroma.Web, :view
15 The definitions below will be executed for every view,
16 controller, etc, so keep them short and clean, focused
17 on imports, uses and aliases.
19 Do NOT define functions inside the quoted expressions
25 use Phoenix.Controller, namespace: Pleroma.Web
27 import Pleroma.Web.Gettext
28 import Pleroma.Web.Router.Helpers
30 plug(:put_layout, Application.get_env(:pleroma, :app_template, "app.html"))
37 root: "lib/pleroma/web/templates",
38 namespace: Pleroma.Web
40 # Import convenience functions from controllers
41 import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
43 import Pleroma.Web.ErrorHelpers
44 import Pleroma.Web.Gettext
45 import Pleroma.Web.Router.Helpers
49 @doc "Same as `render/3` but wrapped in a rescue block"
50 def safe_render(view, template, assigns \\ %{}) do
51 Phoenix.View.render(view, template, assigns)
55 "#{__MODULE__} failed to render #{inspect({view, template})}: #{inspect(error)}"
58 Logger.error(inspect(__STACKTRACE__))
63 Same as `render_many/4` but wrapped in rescue block.
65 def safe_render_many(collection, view, template, assigns \\ %{}) do
66 Enum.map(collection, fn resource ->
67 as = Map.get(assigns, :as) || view.__resource__
68 assigns = Map.put(assigns, as, resource)
69 safe_render(view, template, assigns)
79 # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
81 import Phoenix.Controller
87 # credo:disable-for-next-line Credo.Check.Consistency.MultiAliasImportRequireUse
89 import Pleroma.Web.Gettext
94 When used, dispatch to the appropriate controller/view/etc.
96 defmacro __using__(which) when is_atom(which) do
97 apply(__MODULE__, which, [])
101 Pleroma.Web.Endpoint.url()