|
1 | 1 | defmodule HelloWeb do |
2 | 2 | @moduledoc """ |
3 | | - A module that keeps using definitions for controllers, |
4 | | - views and so on. |
| 3 | + The entrypoint for defining your web interface, such |
| 4 | + as controllers, views, channels and so on. |
5 | 5 |
|
6 | 6 | This can be used in your application as: |
7 | 7 |
|
8 | 8 | use HelloWeb, :controller |
9 | | - use HelloWeb, :view |
| 9 | + use HelloWeb, :html |
10 | 10 |
|
11 | | - The definitions below will be executed for every view, |
12 | | - controller, etc, so keep them short and clean, focused |
| 11 | + The definitions below will be executed for every controller, |
| 12 | + component, etc, so keep them short and clean, focused |
13 | 13 | on imports, uses and aliases. |
14 | 14 |
|
15 | 15 | Do NOT define functions inside the quoted expressions |
16 | | - below. Instead, define any helper function in modules |
17 | | - and import those modules here. |
| 16 | + below. Instead, define additional modules and import |
| 17 | + those modules here. |
18 | 18 | """ |
19 | 19 |
|
| 20 | + def static_paths, |
| 21 | + do: ~w(assets favicon.svg apple-touch-icon.png robots.txt font-files mask-icon.svg) |
| 22 | + |
20 | 23 | def controller do |
21 | 24 | quote do |
22 | | - use Phoenix.Controller, namespace: HelloWeb, log: false |
| 25 | + use Phoenix.Controller, |
| 26 | + namespace: HelloWeb, |
| 27 | + formats: [:html, :json], |
| 28 | + layouts: [html: HelloWeb.Layouts], |
| 29 | + log: false |
| 30 | + |
| 31 | + import Plug.Conn |
| 32 | + import HelloWeb.Gettext |
| 33 | + |
| 34 | + unquote(verified_routes()) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + def component do |
| 39 | + quote do |
| 40 | + use Phoenix.Component |
23 | 41 |
|
24 | | - # Alias the data repository and import query/model functions |
25 | | - alias Hello.Repo |
26 | | - import Ecto |
27 | | - import Ecto.Query |
| 42 | + import HelloWeb.Gettext |
28 | 43 |
|
29 | | - # Import URL helpers from the router |
30 | | - import HelloWeb.Router.Helpers |
| 44 | + # Routes generation with the ~p sigil |
| 45 | + unquote(verified_routes()) |
31 | 46 | end |
32 | 47 | end |
33 | 48 |
|
34 | | - def view do |
| 49 | + def html do |
35 | 50 | quote do |
36 | | - use Phoenix.View, |
37 | | - root: "lib/hello_web/templates", |
38 | | - namespace: HelloWeb |
| 51 | + use Phoenix.Component |
| 52 | + |
| 53 | + # Import convenience functions from controllers |
| 54 | + import Phoenix.Controller, |
| 55 | + only: [get_csrf_token: 0, view_module: 1, view_template: 1] |
39 | 56 |
|
40 | | - alias HelloWeb.Router.Helpers, as: Routes |
| 57 | + # Include general helpers for rendering HTML |
| 58 | + unquote(html_helpers()) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + defp html_helpers do |
| 63 | + quote do |
| 64 | + # Use all HTML functionality (forms, tags, etc) |
| 65 | + use Phoenix.HTML |
| 66 | + # Core UI Components and translation |
| 67 | + import HelloWeb.Gettext |
| 68 | + |
| 69 | + # Routes generation with the ~p sigil |
| 70 | + unquote(verified_routes()) |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + def verified_routes do |
| 75 | + quote do |
| 76 | + use Phoenix.VerifiedRoutes, |
| 77 | + endpoint: HelloWeb.Endpoint, |
| 78 | + router: HelloWeb.Router, |
| 79 | + statics: HelloWeb.static_paths() |
41 | 80 | end |
42 | 81 | end |
43 | 82 |
|
44 | 83 | def router do |
45 | 84 | quote do |
46 | | - use Phoenix.Router |
| 85 | + use Phoenix.Router, helpers: false |
| 86 | + |
| 87 | + # Import common connection and controller functions to use in pipelines |
| 88 | + import Plug.Conn |
| 89 | + import Phoenix.Controller |
47 | 90 | end |
48 | 91 | end |
49 | 92 |
|
50 | 93 | def channel do |
51 | 94 | quote do |
52 | 95 | use Phoenix.Channel |
53 | | - # Alias the data repository and import query/model functions |
54 | | - alias Hello.Repo |
55 | | - import Ecto |
56 | | - import Ecto.Query |
57 | 96 | end |
58 | 97 | end |
59 | 98 |
|
60 | 99 | @doc """ |
61 | | - When used, dispatch to the appropriate controller/view/etc. |
62 | | - """ |
| 100 | + When used, dispatch to the appropriate controller/view/etc. |
| 101 | + """ |
63 | 102 | defmacro __using__(which) when is_atom(which) do |
64 | 103 | apply(__MODULE__, which, []) |
65 | 104 | end |
|
0 commit comments