web.ex 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. defmodule Hello.Web do
  2. @moduledoc """
  3. A module that keeps using definitions for controllers,
  4. views and so on.
  5. This can be used in your application as:
  6. use Hello.Web, :controller
  7. use Hello.Web, :view
  8. Keep the definitions in this module short and clean,
  9. mostly focused on imports, uses and aliases.
  10. """
  11. def model do
  12. quote do
  13. use Ecto.Model
  14. end
  15. end
  16. def controller do
  17. quote do
  18. use Phoenix.Controller
  19. # Alias the data repository and import query/model functions
  20. alias Hello.Repo
  21. import Ecto.Model
  22. import Ecto.Query
  23. # Import URL helpers from the router
  24. import Hello.Router.Helpers
  25. end
  26. end
  27. def view do
  28. quote do
  29. use Phoenix.View, root: "web/templates"
  30. # Import convenience functions from controllers
  31. import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]
  32. # Import URL helpers from the router
  33. import Hello.Router.Helpers
  34. # Import all HTML functions (forms, tags, etc)
  35. use Phoenix.HTML
  36. end
  37. end
  38. def router do
  39. quote do
  40. use Phoenix.Router
  41. end
  42. end
  43. def channel do
  44. quote do
  45. use Phoenix.Channel
  46. # Alias the data repository and import query/model functions
  47. alias Hello.Repo
  48. import Ecto.Model
  49. import Ecto.Query, only: [from: 2]
  50. end
  51. end
  52. @doc """
  53. When used, dispatch to the appropriate controller/view/etc.
  54. """
  55. defmacro __using__(which) when is_atom(which) do
  56. apply(__MODULE__, which, [])
  57. end
  58. end