![]() # Rendering a table from LiveView or controller (1.7) Now, both kinds of content can use the new component approach: # Rendering a table from a controller-based template (pre 1.7) Previously, sub-templates would be rendered differently in a controller vs. One of the most important changes is the introduction of unified function components that work in both LiveViews and in static-rendered controllers. With Phoenix 1.7, these differences are resolved with unified APIs using Phoenix.Template instead of Phoenix.View. In the past, the two different approaches to rendering had different APIs and conventions making it difficult to reuse templates and other functionality between the two. For example, you might use a controller-based static approach for a login page, but an interactive LiveView once the user is logged in. In practice, many applications use both approaches, depending on the type of content being rendered. Phoenix offers two distinct ways to render content: traditional static HTML, and LiveView for building rich, dynamic web applications. Phoenix 1.7 introduces a new Phoenix.Template rendering system which unifies how rendering works between static HTML and LiveView content. Unified Rendering for LiveView & Static HTML The stream_insert/4 function handles both new records and updates, and also supports re-ordering of existing records. If the contents of the collection change, you can use stream_insert/4 or stream_delete/3. Then, in then template, instead of accessing a assign, you simply use instead. To create a stream, call stream/3, providing the socket, the name of the stream, and the initial dataset: def mount(_params, _session, socket) do They also helps to reduce the amount of re-rendering required to modify items on the frontend. LiveView Streams allow you to manage updates, inserts and deletes to large datasets without needing to store and reload the entire collection on the server as one would with a regular LiveView assignment. "/pages/landing?posts=1&search=elixir" LiveView StreamsĪ neat new feature to LiveView in 1.7 is the addition of streams. It also allows us to add query params the way we would with the route helpers: iex> query = This is where verified routescome in: using Elixir's sigil syntax, we can reference a route with ~p in front of a string and take advantage of compile-time route validation. (In fact, as an example, the path used above is incorrect, but we'd have no way of knowing!) There are no errors or warnings to tell you you’re using a broken path. However, writing the path like this comes with a major downside: you have no way of knowing at build-time if you get the path wrong or if it changes later on. It would be far simpler to just refer to the path directly when creating a link: In practice, using helpers to generate URLs often involves consulting the router.ex file or using mix phx.routes to make sure you get the right module and action names. ![]() That works well, but can sometimes be a pain: it’s verbose, and requires knowledge of exactly how routes and actions are named. _path(conn_or_endpoint, :show, "landing", options) ![]() Get "/pages/:page", PageController, :show For example, if we had a PageController defined in our router, and wanted to generate a link to it from somewhere else in the app, we might have code like the following: # router.ex When creating internal links in previous versions of Phoenix, we would typically use a route helperto generate a URL fragment for us. ![]() Verified routes are a new & greatly improved way to construct links within your Phoenix application. Though LiveView already provided a great developer experience, this release feels like "LiveView 2.0" in terms of the well thought out API changes and improvements. In this post, we'll take a look at the new features in Phoenix version 1.7, and why they matter. Version 1.7 of Phoenix is now live, and it's packed with great new features, especially for LiveView. And because it's built on the Erlang/OTP, Elixir and Phoenix bring kick-ass support for scalability, fault tolerance and concurrency right out of the box. It packs a ton of powerful features like channels for real-time websocket based communication, and LiveView, a server-rendered technology which enables the development of real-time interactive apps in the browser, without writing a line of JavaScript. In case you haven't used it before, the Phoenix Framework is an amazingly powerful web development framework for the Elixir programming language. ![]()
0 Comments
Leave a Reply. |