In Phoenix.LiveView, Phoenix.View was replaced by Phoenix.Component. More about format encoders in Phoenix.Template documentation. Phoenix ships with some template engines and format encoders, whichĬan be further configured in the Phoenix application. json extension, Phoenix knows how toĮncode the map returned for the "user.json" template into an actual For example, if you want to render JSON data, weĬould do so by adding a "show.json" entry to render/2 in our view: defmodule YourAppWeb.UserView do use YourAppWeb, :view def render ( "show.json", % )īecause the template has the. This inner representation allows us to separate how templates render and render ( YourApp.UserView, "index.html", name : "John Doe" ) #=>. While we got a string at the end, that's not actually what our templates render_to_string ( YourApp.UserView, "index.html", name : "John Doe" ) #=> "Hello John Doe" In the previous section we have rendered the "index.html" template: Phoenix.View. render_to_string ( YourApp.UserView, "index.html", name : "John Doe" ) #=> "Hello John Doe"Ī template has a name, which also contains a format. After it isĬompiled, the template can be rendered as: Phoenix.View. To compile the code in the file into Elixir source code. heex extension maps to a template engine which tells Phoenix how Template: # your_app_web/templates/user/ Hello Phoenix.View will automatically load all templates at "your_app_web/templates/user"Īnd include them in the YourApp.UserView. Then you could use the definition above to define any view in your application: defmodule YourAppWeb.UserView do use YourAppWeb, :view endīecause we defined the template root to be "lib/your_app_web/templates", def view do quote do use Phoenix.View, root : "lib/your_app_web/templates", namespace : YourAppWeb # Import convenience functions from controllers import Phoenix.Controller, only : # Use all HTML functionality (forms, tags, etc) use Phoenix.HTML import YourAppWeb.ErrorHelpers import YourAppWeb.Gettext end end #. It generally looked like this: defmodule YourAppWeb do #. In Phoenix v1.6 and earlier, new Phoenix apps defined a blueprint for viewsĪt lib/your_app_web.ex. See the "Replaced by Phoenix.Component" section below. Out of fashion in favor of Phoenix.Component, even in non LiveView With design patterns introduced by Phoenix.LiveView, this module has fallen Settings View Source Phoenix.View (phoenix_view v2.0.2)Ī module for generating render/2 functions from templates on disk.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |