Hello, I am trying to render a template without using @conn
. I’ve come up with this but it breaks where my default templates are stored in web. Is there a way to achieve this without creating a toplevel folder just for my partials?
I’m using drab like this:
commander
def show_invoice(socket, dom_sender) do
invoice_id = socket |> select(:val, from: "#invoice_id")
# build the invoice for job
# get invoice
invoice = Broker.Repo.get_by(Broker.Invoice, id: invoice_id) |> Broker.Repo.preload(:customer)
html = Broker.InvoiceController.buildinvoicehtml(invoice)
# create the html
socket |> update(:html, set: html , on: "#main")
end
and the controller I’m calling
def buildinvoicehtml(invoice) do
customer = invoice.customer |> Broker.Repo.preload(:profile)
profile = customer.profile
Broker.PartialView.invoice(invoice,profile)
end
view
defmodule Broker.PartialView do
use Broker.Web, :view
require EEx
EEx.function_from_file :def, :invoice, "partials/invoice.html.eex", [:invoice, :customer]
end
Technically in a phoenix view you can call the render function directly without conn, or replacing conn with whatever you want, or just nil if you do not use it in your templates at all. Phoenix only puts it in the default call for ease of use in accessing @conn
inside the templates but it is not necessary if you call it straight.
The render functions are on your specific phoenix ‘View’ modules, the first argument is the ‘name’ of the template (like “blah.html” or whatever) and the second is the assigns as I recall, of which you can pass in whatever you want.
i have tried this like
defmodule Broker.PageCommander do
use Drab.Commander
import Plug.Conn
def start_quote(socket,sender) do
addresslist = %{ beginaddress: xxx, endaddress: xxx,fromaddress: xxx, toaddress: xxx, beginaddressappt: xxx , endaddressappt: xxx }
html = Broker.PageController.buildaddresshtml(addresslist)
end
defmodule Broker.PageController do
use Broker.Web, :controller
def buildaddresshtml(addresslist) do
%{ beginaddress: beginaddress, endaddress: endaddress, toaddress: toaddress, fromaddress: fromaddress } = addresslist
render nil, "address.html", beginaddress: beginaddress, endaddress: endaddress, toaddress: lastaddress, fromaddress: firstaddress
end
end
to access /web/templates/page/address.html
but the error i get is
failed with the following exception:
** (FunctionClauseError) no function clause matching in Plug.Conn.put_private/3
(plug) lib/plug/conn.ex:354: Plug.Conn.put_private(nil, :phoenix_template, "address.html")
(phoenix) lib/phoenix/controller.ex:639: Phoenix.Controller.do_render/4
i have also tried importing plug.conn into the commander and passing Plug.Conn to the pagecontroller method as a conn var still the same error