37 lines
1 KiB
Elixir
37 lines
1 KiB
Elixir
defmodule RlRepo.Template do
|
|
require Logger
|
|
|
|
def template(template_name, replacements) do
|
|
Logger.info("Loading template #{template_name}")
|
|
{:ok, body} = File.read("assets/html/#{template_name}.html")
|
|
|
|
inner_template(body, replacements)
|
|
end
|
|
|
|
def inner_template(body, replacements) do
|
|
# TODO: Check if there is any actual placeholders
|
|
# v = String.contains?(body, "~(")
|
|
len = body |> String.split("~(") |> length()
|
|
template_step(replacements, body, len)
|
|
end
|
|
|
|
def template_step(replacements, body, steps_left) do
|
|
[before_placeholder | rest] = String.split(body, "~(", parts: 2)
|
|
[remains | _] = rest
|
|
[placeholder_name | after_placeholder] = String.split(remains, ")", parts: 2)
|
|
[after_placeholder | _] = after_placeholder
|
|
|
|
{:ok, replace} = Map.fetch(replacements, placeholder_name)
|
|
|
|
body = before_placeholder <> replace <> after_placeholder
|
|
|
|
steps_left = steps_left - 1
|
|
|
|
if steps_left == 1 do
|
|
body
|
|
else
|
|
template_step(replacements, body, steps_left)
|
|
end
|
|
end
|
|
end
|