 
   defp do_load_templates(name, state) do
     path = "#{state.src}templates/#{name}.html.eex"
-    case compile_template path, state do
-      {:ok, ast} -> {:ok, {name, ast}}
-      {:error, _, _} = error -> error
+    with {:ok, data} <- File.read(path),
+         {:ok, ast} <- compile_template(data, state)
+    do
+      {:ok, {name, ast}}
+    else
+      {:error, reason} -> {:error, :file_error, {reason, path, 0}}
+      {:error, msg, line} -> {:error, :invalid_template, {msg, path, line}}
     end
   end
 


  defp do_load_includes(name, state) do
    path = "#{state.src}includes/#{name}.html.eex"
    case compile_template path, state do
      {:ok, ast} -> {:ok, {name, ast}}
      {:error, _, _} = error -> error
    end
  end


    error
  end

  @spec compile_template(binary, state)
    :: {:ok, Macro.t}
     | {:error, binary, integer}

  def compile_template(data, state) do
    try do
      ast = data |> EEx.compile_string() |> preprocess_template(state)
      {:ok, ast}
    rescue
      e in EEx.SyntaxError ->
        {:error, e.message, e.line}
      e in SyntaxError ->
        {:error, e.description, e.line}
      e in TokenMissingError ->
        {:error, e.description, e.line}
    end
  end

