web.ex 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.Schema
  14. import Ecto
  15. import Ecto.Changeset
  16. import Ecto.Query
  17. end
  18. end
  19. def controller do
  20. quote do
  21. use Phoenix.Controller, log: false
  22. # Alias the data repository and import query/model functions
  23. alias Hello.Repo
  24. import Ecto
  25. import Ecto.Query
  26. # Import URL helpers from the router
  27. import Hello.Router.Helpers
  28. end
  29. end
  30. def view do
  31. quote do
  32. use Phoenix.View, root: "web/templates"
  33. # Import convenience functions from controllers
  34. import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2,
  35. action_name: 1, controller_module: 1]
  36. # Import all HTML functions (forms, tags, etc)
  37. use Phoenix.HTML
  38. import Hello.Router.Helpers
  39. end
  40. end
  41. def router do
  42. quote do
  43. use Phoenix.Router
  44. end
  45. end
  46. def channel do
  47. quote do
  48. use Phoenix.Channel
  49. # Alias the data repository and import query/model functions
  50. alias Hello.Repo
  51. import Ecto
  52. import Ecto.Query
  53. end
  54. end
  55. @doc """
  56. When used, dispatch to the appropriate controller/view/etc.
  57. """
  58. defmacro __using__(which) when is_atom(which) do
  59. apply(__MODULE__, which, [])
  60. end
  61. end