TEMPLATE: Implemented templates

This commit is contained in:
Able 2025-02-18 12:19:14 -06:00
parent 4de928e01e
commit 60e0e64b56
3 changed files with 21 additions and 18 deletions

View file

@ -7,7 +7,7 @@
<body>
<center>
<h1>404 Not Found</h1>
<p>Reason: ~(reason) </p>
<p>Some Reason: ~(reason)</p>
</center>
</body>
</html>
</html>

View file

@ -4,7 +4,7 @@ defmodule RlRepo.Router do
"""
def status_404() do
placeholders = %{ "reason" => "Unknown"}
placeholders = %{"reason" => "Unknown"}
{404, "text/html", RlRepo.Template.template("404-error", placeholders)}
end

View file

@ -5,25 +5,28 @@ defmodule RlRepo.Template do
Logger.info "Loading template #{template_name}"
{:ok, body} = File.read("assets/html/#{template_name}.html")
# TODO: Find all template start '~(' and all template ends ')'
[before_placeholder | rest] = split_path = String.split(body, "~(");
[abc | _] = rest
[placeholder_name | after_placeholder] = String.split(abc, ")");
len = body |> String.split("~(") |> length()
template_step(replacements, body, len)
end
def template_step(replacements, body, steps_left) do
[before_placeholder | rest] = split_path = String.split(body, "~(", parts: 2);
[remains | _] = rest
[placeholder_name | after_placeholder] = String.split(remains, ")", parts: 2);
[after_placeholder | _ ] = after_placeholder
# Logger.info rest
{:ok, replace} = Map.fetch(replacements, placeholder_name)
# next = String.split(rest, ")");
body = before_placeholder <> replace <> after_placeholder
Logger.info "Replacing #{placeholder_name} with #{replace}"
[after_placeholder| _] = after_placeholder
a = before_placeholder <> replace <> after_placeholder
# TODO: use the map to populate placeholders
a
# body
steps_left = steps_left - 1
if steps_left == 1 do
body
else
template_step(replacements, body, steps_left)
end
end
end