Packages
drab
0.4.1
0.10.5
0.10.4
0.10.3
0.10.2
0.10.1
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.3
0.8.2
0.8.1
0.8.0
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.3
0.6.2
0.6.1
0.6.0
0.6.0-pre.1
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.1
0.1.0
Remote controlled frontend framework for Phoenix.
Current section
Files
Jump to
Current section
Files
lib/drab/template.ex
defmodule Drab.Template do
require IEx
require Logger
@drab_templates "priv/templates/drab"
@moduledoc false
# compiling internal templates only
# TODO: find a way to compile also user additional templates
Logger.info "Compiling Drab Templates"
drab_templates = Path.join(@drab_templates, "*") |> Path.wildcard()
for template_with_path <- drab_templates do
@external_resource template_with_path
filename = Path.basename(template_with_path)
compiled = EEx.compile_file(template_with_path) |> Macro.escape
defp compiled_template(unquote(filename)) do
unquote(compiled)
end
end
# catch-all is to give a warning when file not found
defp compiled_template(filename) do
raise "Can't find the template `#{filename}` in `#{user_templates()}`"
end
@doc false
def render_template(filename, bindings) do
# TODO: this is not very efficient, as it searches for a template every single time
p = Path.join(user_templates(), filename)
if p |> File.exists? do
EEx.eval_file(p, bindings)
else
{result, _} = Code.eval_quoted(compiled_template(filename), bindings)
result
end
end
# defp find_template(filename) do
# sources = template_paths() |> Enum.map(&(Path.join(&1, filename)))
# Enum.find(sources, &(File.exists?(&1))) ||
# raise "Can't find the template `#{filename}` in `#{user_templates()}`"
# end
# defp template_dir(app) when is_atom(app) do
# Application.app_dir(app) |> Path.join(@drab_templates)
# end
# defp template_dir(path) when is_binary(path) do
# # Path.join(path, @drab_templates)
# Path.join(".", path)
# end
# defp paths(), do: [user_templates(), :drab]
# defp template_paths(), do: Enum.map(paths(), &(template_dir(&1)))
defp user_templates(), do: Drab.Config.get(:templates_path)
end