tj0
I’ve been following the steps here for the upgrade from 1.6 to 1.7 and it has gone relatively smoothly all the way till the phoenix_view to phoenix_component switch. verified_routes are quite nifty.
In the past, I’ve found the liveview upgrades / breaking changes to be extremely valuable. I’m not quite sure about the phoenix changes though, but since I’m not sure phoenix_view will be deprecated in the future, I’m willing to put in the legwork to do the migration.
To summarize for those considering the upgrade and all the layout changes:
- there is no views directory. Not sure where any functions are supposed to go that were in views.
- liveview templates are colocated in the liveview directory or embedded
- livecomponent templates are…I’m not sure where.
The two main issues I’m having are
- The templates for my LiveViews and LiveComponents are from 200-600 lines. These are far too large to embed into the render/1 function and I’m not sure how to pass on the assigns to the template.
Original
import AppWeb.ChartView
def render(assigns) do
AppWeb.ChartView.render("show.html", assigns)
end
New one where it’s not indicated how to pass the assigns into the
use AppWeb, :live_view
embed_templates "../templates/chart/*"
def render(assigns) do
# does not pass the assigns into the show and if they are passed as assigns={assigns} will need a template re-write
~H"""
<.show />
"""
end
I was hoping for something like the below code, but I cannot call render/2 or Phoenix.LiveView.render/2 because “(module Phoenix.Liveview is not available)”
def render(assigns) do
~H"""
<%= Phoenix.LiveView.render "show.html", assigns %>
"""
end
- I’ve put rendering specific functions into the views. App.ChartView has functions for making viewable dates, converting decimals to fractions, capitalization, etc.
I like the idea of getting rid of the views directory as it seemed like mostly boilerplate. However, I can’t figure out how to do an easy migration of the templates to use phoenix_component instead of phoenix_views.
I’m not understanding how to replace the render functions in a way that doesn’t require massive changes to the templates or how to pass in the helper functions for the view itself. What am I missing?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
aiwaiwa
My little experiment of merging
index.exandshow.exfrom the standard live generator ended up looking like this:where
index.indexandindex.showcorrespond toindex.index.html.heexandindex.show.html.heexfiles.codeanpeace
Maybe something like:
<%= ChartView.show(assigns) %>From Migrating to Phoenix.Component | Phoenix.View:
And from Compartmentalize state, markup, and events in LiveView | Phoenix.LiveView:
codeanpeace
As a heads up, this is a known pitfall:
source: Pitfalls | Assigns and HEEx templates | Phoenix LiveView v0.18.18
aiwaiwa
Thank you! Does modifying assigns fall under this category? I saw this pattern in core components
codeanpeace
Hmm, that’s a good point – not sure if modifying assigns would fall under this. Maybe it’s fine for core components since they’re invoked within the ~H sigil… ¯\_(ツ)_/¯
If I’m understanding the intent correctly, it might still make sense for those
assign_newcalls to be outsiderenderas the assigns that it ensures exists shouldn’t change across re-renders.josevalim
FWIW, Phoenix View is not deprecated and we are committed to keeping it working in the long term. I will make the docs clearer.
tj0
After a bit of fiddling with the syntax, the following seems to work (thanks @codeanpeace , your suggestion was pretty close).
The existing template has a root div element, however, without wrapping the ~H in a div, we " Stateful components must have a single static HTML tag at the root".
Still not sure how to do conditional rendering yet.
I’m relieved as I’m not sure how risky this upgrade is to be honest. Might have a lot of little gotchas and needs extensive testing.
aiwaiwa
So just to reiterate, since
core_componentsdefine stateless components, and they modify assigns for themselves to render correctly (here’s an example):This differs from my conditional rendering because it implemenets the
rendercallback of a:live_view?I’m just trying to formulate a rule of a thumb for such scenarios.
By the way, the suggested
strangely does not include
<% index_show(assigns) %>at all, just shows an empty page, although it sees embedded component functions. Anything else put within condition shows up, but not the<% index_show(assigns) %>. And all I did was rename the files to eliminate the..tj0
The main issue from my understanding is ensuring updates and change tracking works correctly. In this case, the table is adding row ids based on changing data. The rest of the code isn’t there, but there are two possibilities:
Without testing it, I think your code should work for change tracking. The reason the second conditional doesn’t render is that it needs a <%= , not <% to display.
aiwaiwa
LOL you are right! Thanks!