web.ex 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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,
  32. action_name: 1, controller_module: 1]
  33. # Import URL helpers from the router
  34. import Hello.Router.Helpers
  35. # Import all HTML functions (forms, tags, etc)
  36. use Phoenix.HTML
  37. end
  38. end
  39. def router do
  40. quote do
  41. use Phoenix.Router
  42. end
  43. end
  44. def channel do
  45. quote do
  46. use Phoenix.Channel
  47. # Alias the data repository and import query/model functions
  48. alias Hello.Repo
  49. import Ecto.Model
  50. import Ecto.Query, only: [from: 2]
  51. end
  52. end
  53. @doc """
  54. When used, dispatch to the appropriate controller/view/etc.
  55. """
  56. defmacro __using__(which) when is_atom(which) do
  57. apply(__MODULE__, which, [])
  58. end
  59. end