Skip to content

Instantly share code, notes, and snippets.

@mjs2600
Created December 23, 2013 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjs2600/8101839 to your computer and use it in GitHub Desktop.
Save mjs2600/8101839 to your computer and use it in GitHub Desktop.
Weber view extensions
# views_loader.ex
defmacro compile_views(root) do
root = Code.eval_quoted(root, [], file: __ENV__.file, line: __ENV__.line) |> elem(0)
views = Enum.filter(get_all_files(:erlang.binary_to_list(root) ++ '/lib/views/'), fn(f) -> :filename.extension(f) == '.html' end)
lc view inlist views do
content = add_helpers_imports(File.read!(view))
quote do
defmodule unquote(Weber.Utils.build_module_name(view)) do
require EEx
EEx.function_from_string(:def, :render_template, unquote(content), [:assigns])
def __view__, do: unquote(content)
end
end
end
end
# weber_req_handler_result.ex
defp request({:render, data, headers}, app) do
filename = List.last(Module.split app.controller) <> ".html"
file_content = find_file_path(Weber.Path.__views__, filename) |> elem(1)
case :lists.keyfind(:__layout__, 1, app.controller.__info__(:functions)) do
false ->
{:render, 200, file_content.render_template(:lists.append(data, [conn: app.conn])), headers}
_ ->
content = file_content.__view__
Weber.Helper.ContentFor.content_for(:layout, app.controller.__layout__)
{:render, 200, EEx.eval_string(content, []), headers}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment