vegabook

vegabook

How to serve an img with Phoenix?

I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here is what my /web/templates/page/index.html.eex looks like:

<div class=whitext>
  <h2><%= gettext ">>> %{name}", name: "CRVM" %></h2>
  <p> A new perspective on fixed income relative value.</p>
</div>

<div>
  <p> hello </p>
  <img src="/web/static/assets/images/crvmlogowhite.svg"/>
</div>

But nothing is showing up (though “hello” is). So a) what is the canonical way of putting an image onto a page in Elixir, and b) if the above is okay, how is the image referenced? By the way I have tried multiple versions of the path. If I use it as a css background then url(“/images/crvmlogowhite.svg”) does work.

Most Liked

alexgaribay

alexgaribay

Phoenix Core Team

Your static assets can be served by using the static_path routes. You’ll want to use src instead of source attribute for the file source for your image. But here’s how it should look:

<img src="<%= static_path(@conn, "/images/crvmlogowhite.svg") %>">

Keep in mind with the default configuration of brunch, your asset files are copied to priv/static/ and are served from there. Your endpoint.ex file defines what static files should be served from that directory.

andrejsm

andrejsm

Everything under priv/static is served from /. To link to priv/static/images/photo.jpg in CSS you write url(/images/photo.jpg).

You don’t need to use static_path obligatory. If you are configuring to serve assets from a CDN, then yes, you would need to simplify the development process.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement