MatijaL

MatijaL

Hello,

I have a plug which adds a layout to some controller functions.

With Phoenix 1.6, I wrote it like this and it worked fine.
plug :put_layout "account.html" when action in [:edit, :update]

Phoenix 1.7 introduced a new way of writing it, it should be written something like this:
plug :put_layout, html: {TestApp.LayoutView, :account}

but now I can’t use when clause.

Can anyone suggest a proper way of writing this plug?

Showing Posts 21 to 12

tmbb

tmbb

Ok, I agree it’s fine then :blush:

josevalim

josevalim

Creator of Elixir

It is not backwards incompatible. The scenario we warn only happens when you mix the new style (which by definition did not exist before and by definition cannot be incompatible) with the old style. If you are getting the warning, it means you are mixing both styles and certainly one of them is not working as expected.

LostKobrakai

LostKobrakai

How is adding a warning backward incompatible?

tmbb

tmbb

Isn’t this a backward incompatible change? Should we have these between minor versions?

MatijaL

MatijaL OP

I just tried it and it works. Thanks a lot for taking the time to solve it and getting back to me.

josevalim

josevalim

Creator of Elixir

We found a potential cause for this. If you are using plug :put_layout, html: ..., you need to makes sure that you declare the default layout and its format in your use Phoenix.Controller, layouts: [html: {MyDefaultLayout, :app}].

We have introduced a warning message on a soon to be released new patch version of Phoenix.

Here is the issue: Phoenix 1.7 Controller.put_layout @spec does not allow 1.6 usage · Issue #5320 · phoenixframework/phoenix · GitHub

Here is the commit: Add warning on mixed layout usage, closes #5320 · phoenixframework/phoenix@b2f110f · GitHub

MatijaL

MatijaL OP

Thanks for checking it out… I checked the docs and updated the code but as I changed it, something else broke and when I updated that piece of code again something new broke so eventually I gave up. The thing is that I don’t use LiveView but regular views, my plan is to move to LiveView eventually but that requires some time so for now I went back to the old code. Phoenix 1.7 is backward compatible so ElixirLS is giving me errors but that it, the code works just fine.

codeanpeace

codeanpeace

Could it have to do with the shift from use MyAppWeb, :view to use MyAppWeb, :html and the new function embed_templates?

# in v1.6
defmodule YourAppWeb do
  # ...

  def view do
    quote do
      use Phoenix.View, root: "lib/your_app_web/templates", namespace: YourAppWeb
      ...
    end
  end
  ...
end

defmodule YourAppWeb.UserView do
  use YourAppWeb, :view
end

In Phoenix.LiveView, Phoenix.View was replaced by Phoenix.Component. With Phoenix v1.7+ we can also use Phoenix.Component to render traditional templates as functional components, using the embed_templates function.

For example, in Phoenix v1.7+, the YourAppWeb.UserView above would be written as:

defmodule YourAppWeb.UserHTML do
  use YourAppWeb, :html

  embed_templates "users/*"
end


Feature: To embed templates from disk
Phoenix v1.6: use Phoenix.View
Phoenix v1.7: use Phoenix.Component (+ embed_templates)

source: Phoenix.View docs

MatijaL

MatijaL OP

I don’t think I ever even touched that file except when upgrading to 1.7

defmodule TestAppWeb do
  @moduledoc """
  The entrypoint for defining your web interface, such
  as controllers, views, channels and so on.

  This can be used in your application as:

      use TestAppWeb, :controller
      use TestAppWeb, :view

  The definitions below will be executed for every view,
  controller, etc, so keep them short and clean, focused
  on imports, uses and aliases.

  Do NOT define functions inside the quoted expressions
  below. Instead, define any helper function in modules
  and import those modules here.
  """

  def static_paths, do: ~w(assets fonts images favicon robots.txt)

  def controller do
    quote do
      use Phoenix.Controller, namespace: TestAppWeb

      import Plug.Conn
      import TestAppWeb.Gettext
      alias TestAppWeb.Router.Helpers, as: Routes

      unquote(verified_routes())
    end
  end

  def view do
    quote do
      use Phoenix.View,
        root: "lib/TestApp_web/templates",
        namespace: TestAppWeb

      # Import convenience functions from controllers
      import Phoenix.Controller,
        only: [get_flash: 1, get_flash: 2, view_module: 1, view_template: 1]

      # Include shared imports and aliases for views
      unquote(view_helpers())
    end
  end

  def verified_routes do
   quote do
      use Phoenix.VerifiedRoutes,
        endpoint: TestAppWeb.Endpoint,
        router: TestAppWeb.Router,
        statics: TestAppWeb.static_paths()
    end
  end

  def live_view do
    quote do
      use Phoenix.LiveView,
        layout: {TestAppWeb.LayoutView, "live.html"}

      unquote(view_helpers())
    end
  end

  def live_component do
    quote do
      use Phoenix.LiveComponent

      unquote(view_helpers())
    end
  end

  def component do
    quote do
      use Phoenix.Component

      unquote(view_helpers())
    end
  end

  def router do
    quote do
      use Phoenix.Router

      import Plug.Conn
      import Phoenix.Controller
      import Phoenix.LiveView.Router
    end
  end

  def channel do
    quote do
      use Phoenix.Channel
      import TestAppWeb.Gettext
    end
  end

  defp view_helpers do
    quote do
      # Use all HTML functionality (forms, tags, etc)
      use Phoenix.HTML

      # Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc)
      import Phoenix.Component

      # Import basic rendering functionality (render, render_layout, etc)
      import Phoenix.View

      import TestAppWeb.ErrorHelpers
      import TestAppWeb.Gettext
      alias TestAppWeb.Router.Helpers, as: Routes

      unquote(verified_routes())
    end
  end

  @doc """
  When used, dispatch to the appropriate controller/view/etc.
  """
  defmacro __using__(which) when is_atom(which) do
    apply(__MODULE__, which, [])
  end
end
josevalim

josevalim

Creator of Elixir

What about your my_app_web.ex file? Maybe something in there?

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews