woutdp / live_svelte

Svelte inside Phoenix LiveView with seamless end-to-end reactivity
https://hexdocs.pm/live_svelte
MIT License
1.01k stars 38 forks source link

~V without assigns #41

Closed dev-guy closed 1 year ago

dev-guy commented 1 year ago

I'm using ~V for a dead view that doesn't use assigns. The compiler complains that assigns is defined but not used. So I rename it to _assigns. Then I get the error:

** (RuntimeError) ~H requires a variable named "assigns" to exist and be set to a map
    (phoenix_live_view 0.18.18) expanding macro: Phoenix.Component.sigil_H/2
    lib/kantan_web/controllers/svelte/navigation.ex:6: KantanWeb.Svelte.Navigation.render/2
    (live_svelte 0.5.1) expanding macro: LiveSvelte.sigil_V/2
    lib/kantan_web/controllers/svelte/navigation.ex:6: KantanWeb.Svelte.Navigation.render/2
woutdp commented 1 year ago

Can you give a code example, will be easier for me to debug.

dev-guy commented 1 year ago

https://github.com/dev-guy/ash-svelte-flowbite/blob/ad7d00fec03a8c2017076fd9283f50af2122102a/lib/kantan_web/controllers/svelte/navigation.ex

This link was broken a few seconds ago, please retry.

dev-guy commented 1 year ago

It's not happening any more. Is it a requirement that ~V is used with a live view?

woutdp commented 1 year ago

I'm trying it out, the way I see it is:

All this being said, it might be interesting to look into how to render Svelte templates directly, this way we avoid the "one line template" like shown here: https://github.com/dev-guy/phoenix-ash-svelte-flowbite/blob/ad7d00fec03a8c2017076fd9283f50af2122102a/lib/kantan_web/controllers/page_html/tiptap.html.heex

Let me know if that makes sense, I might be wrong here btw :)

dev-guy commented 1 year ago

@woutdp yes, that’s exactly what I’m trying to do for dead views. I’m trying to get rid of the one line file. I thought sigil V would do that. However, I still need a way to disable SSR. I frankly don’t know enough about Phoenix and liveview to know what a component is versus a non-component. Assume I know nothing. If I must use liveview with the sigil, that’s fine. Live views just feel heavy.

woutdp commented 1 year ago

Disabling SSR is a good point, I would suggest for now only using ~V inside a LiveView or LiveComponent. For Functional components and dead views, I'd recommend using <LiveSvelte... syntax.

I think I need some more time to think about how ~V would interact properly with Functional components as passing options such as disabling SSR is not straightforward I think. It might be possible though. Same with dead views.

So for now I'd recommend using the ~V sigil only in LiveViews and LiveComponents.

As for the difference between components and non-components. You can think of components just like in Svelte, snippets of reusable code. Non components are views, so it would encapsulate the entire page (or in the case of LiveView, can be embedded in a dead view layout)