rhcarvalho
Hi,
In the past when I needed to set the background image on an element I gave up Verified Routes and wrote something like bg-[url(/images/background.svg)] in my template. The Tailwind compiler then generates a CSS class that sets the background to the image.
I found myself in need of doing it again, and since I couldn’t find an existing thread on using TailwindCSS + VerifiedRoutes I decided to ask – is there a way to use both together?
I tried this but it doesn’t work, because I understand the Tailwind compiler sees Elixir code as is and not the compiled string after the ~p sigil is processed:
<div class={"bg-[url(#{~p"/images/background.svg"})]"}>...</div>
Another idea, which I did not try, would be to add some kind of plugin in tailwind.config.js similar to how Heroicons are made available. I thought perhaps someone in the community has an easier way to offer?
Trending in Questions
Other Trending Topics
Latest Phoenix Threads
Latest on Elixir Forum
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
codeanpeace
Hmm, wonder if the nested double quotes are the problem…
class={"bg-[url(" + ~p"/images/background.svg" + ")]"}class={"bg-[url(#{~p'/images/background.svg'})]"}Just to rule that out, I’d give those a shot ¯\_(ツ)_/¯
rhcarvalho
Tailwind only looks at the text of the files, without any further interpretation: Detecting classes in source files - Core concepts - Tailwind CSS, so quoting differences don’t matter.
rhcarvalho
There are a few possibilities listed in Detecting classes in source files - Core concepts - Tailwind CSS.
I could:
~psigil;Considering that Verified Routes for static paths don’t really verify that the full path exists in disk, but only that the path is well-formed, then I think the possibilities above are not worth it – simply don’t use a verified route for cases like in my OP.
I will feel good if other people share that’s what they do
harmon25
My solution to a similar problem was to use css variables and to extend tailwind background image:
this requires setting the
--bg-imagecss variable tourl(/some/image.png)which can be done with an inline style, kinda like this:rhcarvalho
A bit of going on a tangent, but something I learned recently is that
[...]can also be used to set CSS variables:So that could be used to set
--bg-imageconditionally, but if the value would beurl(...)then again the URL needs to appear as-is within the parentheses or back to Tailwind not picking up the value to generate CSS classes.arcanemachine
Just spitballing here, but how about just adding it to the safelist since it’s just the one URL?
LostKobrakai
That one works. The style can be dynamically set in that case.
More details: Adding custom styles - Core concepts - Tailwind CSS
rhcarvalho
I like how you put those ideas together
Did you actually reach out for that in existing projects? I’m still finding verified routes may not be worth the trouble in that case.
LostKobrakai
I did use that approach before, though with colors not urls.