Skip to content

Instantly share code, notes, and snippets.

@mhanberg
Created March 9, 2021 18:17
Show Gist options
  • Save mhanberg/c564703fa31a91464ad948e0dd846269 to your computer and use it in GitHub Desktop.
Save mhanberg/c564703fa31a91464ad948e0dd846269 to your computer and use it in GitHub Desktop.
Using Temple directly in live view modules
# lib/my_app/views/live_view.ex
defmodule MyAppWeb.LiveView do
defmacro render(block) do
quote do
def render(var!(assigns)) do
require Temple
Temple.compile(unquote(Phoenix.LiveView.Engine), unquote(block))
end
end
end
end
# lib/my_app_web.ex
defmodule MyAppWeb do
# ...
def live_view do
quote do
use Phoenix.LiveView,
layout: {MyAppWeb.LayoutView, "live.html"}
import MyAppWeb.LiveView
unquote(view_helpers())
end
end
def live_component do
quote do
use Phoenix.LiveComponent
import MyAppWeb.LiveView
unquote(view_helpers())
end
end
# ...
end
# lib/my_app_web/live/page/show.ex
defmodule MyAppWeb.Page.Show do
use MyAppWeb, :live_view
render do
div do
"Hello, world!"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment