Current section
Files
Jump to
Current section
Files
lib/template.ex
defmodule ExWeb.Template do
require Logger
defstruct [:module, :page, :assigns, :rendered_content]
defmacro __using__(opts) do
opts = Map.new(opts)
render_function = Map.get(opts, :render) || :render
quote do
require EEx
@before_compile unquote(__MODULE__)
def unquote(render_function)(template, assigns \\ %{}) do
file_name = Path.rootname(template)
extension = Path.extname(template)
extension = if String.length(extension) < 1 do
".html"
else
extension
end
apply(__MODULE__, :"ex_web_template_#{file_name}#{extension}", [assigns])
end
end
end
defmacro __before_compile__(_env) do
module_name = __CALLER__.module
root_folder = "templates"
[_lang_root_mod, _app_name|folder_modules] = "#{module_name}" |> String.split(".")
inner_folder_name = Enum.map(folder_modules, &String.downcase(&1)) |> Path.join
folder = Path.join(root_folder, inner_folder_name)
Path.wildcard(Path.join(folder, "*.*"))
|> Enum.map(fn (file) ->
Logger.debug "Compiling: #{file}"
basefilename = Path.basename(file) |> String.replace(~r/\.eex$/, "")
file_name = Path.rootname(basefilename)
extension = Path.extname(basefilename)
template_name = "ex_web_template_#{file_name}#{extension}" |> String.replace(~r/\.eex$/, "") |> String.to_atom
quote do
EEx.function_from_file :def, unquote(template_name), unquote(file), [:assigns]
end
end)
end
end