RobinBeekhof
Can I use p-sigil in H-sigil?
Can I use the new p-sigil in a H-sigil?
defmodule MyProjectWeb.Navbar do
use Phoenix.Component
def navbar(assigns) do
~H"""
<div class="navbar bg-primary">
<a href={~p"/"}>Home</a>
</div>
"""
end
end
When I do this I get a compile error
“(CompileError) undefined function sigil_p/2 (expected MyProjectWeb.Navbar to define such a function or for it to be imported, but none are available)”
Am I doing something wrong, or is this not possible?
Most Liked
Johnny5
Switch from
use Phoenix.Component
to
use MyProjectWeb, :html
This should get you working. The MyProjectWeb.html macro imports everything you need.
idursun
A more up-to-date solution is mentioned here:
It showed up as the error:
warning: undefined function sigil_p/2.
The fix was to add the following line to the top of my component file.
use Phoenix.VerifiedRoutes,
endpoint: MyAppWeb.Endpoint,
router: MyAppWeb.Router
sr.jilarious
Funny enough I just ran into the same issue for the first time just now, about 2 hours later. ![]()
I think your and my issue are the same - that we’re trying to create our components in a module that is also included in the html_helpers macro that html unquotes. So you end up with a circular reference.
In RobinBeekhof’s original question, he’s doing this in a singular module MyProjectWeb.Navbar, so it wouldn’t be included as a core component and create the import loop.
I ended up fixing it by creating an extra helper in my myapp_web.ex, called html_helpers_plus which looks like this:
defp html_helpers_plus do
quote do
use Phoenix.Component
# Include general helpers for rendering HTML
unquote(html_helpers())
# Custom application components using verified routes
import MyAppWeb.AppComponents
end
end
I.e. it just unquotes html_helpers and then imports my app components that want to use verified routes. That way in AppComponents I can have the use MyAppWeb, :html line and not get the circular dependency.
I then import that in the live_view macro defined in the same file:
def live_view do
quote do
use Phoenix.LiveView,
layout: {MyAppWeb.Layouts, :app}
unquote(html_helpers_plus())
end
end
and then any of my live views can automatically access the verified routes using components.
In dead views you can still import the components directly in the xyz_html.ex before the embed_templates call.
I’d love to know if there is a more elegant way to solve the issue though ![]()
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









