This repository has been archived on 2024-06-16. You can view files and clone it, but cannot push or open issues or pull requests.
mebe/web/web.ex

77 lines
1.8 KiB
Elixir
Raw Permalink Normal View History

2015-05-14 20:58:25 +00:00
defmodule MebeWeb.Web do
@moduledoc """
A module that keeps using definitions for controllers,
views and so on.
This can be used in your application as:
use MebeWeb.Web, :controller
use MebeWeb.Web, :view
The definitions below will be executed for every view,
controller, etc, so keep them short and clean, focused
on imports, uses and aliases.
Do NOT define functions inside the quoted expressions
below.
"""
2015-09-13 19:34:13 +00:00
alias MebeWeb.Utils
2015-05-14 20:58:25 +00:00
def controller do
quote do
use Phoenix.Controller
# Import URL helpers from the router
import MebeWeb.Router.Helpers
end
end
def view do
quote do
default_root = "web/templates"
2015-09-13 19:34:13 +00:00
# Check if we should be using a custom template
# TODO: Enable custom views even for releases, currently always set to off
# because they use compile time configuration
custom_templates = Utils.get_conf(:custom_templates)
compiled_view = MebeWeb.Web.module_to_str(__MODULE__)
2015-09-13 19:34:13 +00:00
root = if Map.get(custom_templates, compiled_view) do
default_root <> "/custom"
else
default_root
2015-09-13 19:34:13 +00:00
end
use Phoenix.View, root: root
2015-05-14 20:58:25 +00:00
# Import URL helpers from the router
import MebeWeb.Router.Helpers
# Use all HTML functionality (forms, tags, etc)
use Phoenix.HTML
# Custom helpers
import MebeWeb.Utils, only: [get_author: 1, slugify: 1, unslugify_author: 1]
# DateTime functionality
alias Calendar.DateTime
import Calendar.Strftime
2015-05-14 20:58:25 +00:00
end
end
@doc """
When used, dispatch to the appropriate controller/view/etc.
"""
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
2015-09-13 19:34:13 +00:00
def module_to_str(module) do
module
|> Atom.to_string
|> String.split(".")
|> List.last
end
2015-05-14 20:58:25 +00:00
end