Add local images to web
SlowEnter opened this issue · comments
I'm trying to add images to the web loaded directly from the compiled rust code, my question is if this is possible...
Current not working goes like this:
{markup::raw("<img id=\"logo\" src=\"images/my-logo.png\">")}
This works when I replace whats in src with an already uploaded picture on a web (hyperlink) but I would like to have an images dir that I can use.
Rust struct:
my-web
├── src/
│ ├── images/
│ │ ├── my-logo.png
│ ├── index.rs
So my question would be if its possible to load an image in markup.rs from a local folder? If so, how would this be done?
Hi @SlowEnter,
markup.rs only generates the markup (HTML or XML). Loading a file from disk depends entirely on how you're serving this page to the browser. Are you using a web server like actix-web or rocket? With actix-web for example, you'll need to mount the "images" directory to a path like /images
(ref) and then do src="/images/my-logo.png"
.
Also, your current code is equivalent to the following (markup::raw
is meant for injecting arbitrary code without any html escaping):
img#logo[src = "images/my-logo.png"];
(Note the ;
at the end instead of {}
as <img>
is a self-closing tag in HTML.)
Hi @utkarshkukreti,
thanks for the swift response! I see what you mean and will use another crate as you said to mount the "images" dir.