I want to serve static page in Phoenix Framework to use it in Angular Views. I know I can serve regular HTML, but I want to get rid of the default LayoutView
. I could do with a solution to just have some Phoenix Views which don't "inherit" from LayoutView
. Is it possible?
Asked
Active
Viewed 8,334 times
17

Kelu Thatsall
- 2,494
- 1
- 22
- 50
1 Answers
38
You can serve static files by having a file in priv/static
and matching the path in the Plug.Static
options:
plug Plug.Static,
at: "/", from: :hello_phoenix, gzip: false,
only: ~w(css fonts images js favicon.ico robots.txt my_fine.html)
You can also bypass the layout using put_layout/2:
conn
|> put_layout(false)
|> render("index.html")
The put_layout/2
function can also be called as a plug (due to the function arguments). This is useful if you want it to apply to the entire controller:
plug :put_layout, false

Gazler
- 83,029
- 18
- 279
- 245
-
7very helpful, thanks. btw, for future readers, the plug static config is located in /lib/
/endpoint.ex – Tahbaza Aug 16 '16 at 21:01 -
1I want to serve an index.html from the endpoint /apidocs. I want to put the static files in /priv/static/swagger. I'm having trouble deducing from the above answer how to do that. – raarts Dec 31 '16 at 16:43
-
@raarts yeah, what the first part of the answer would do is allow you to then request "/swagger/index.html" and you'd get the docs, but the full path to the file would be required. I'm trying to find out kinda the same thing myself. – Ilkka Feb 08 '17 at 20:33
-
@raarts found it, you want to do like http://stackoverflow.com/a/37924920/15064. Create a controller with a method that has that code, use a path relative to the root of the codebase (i.e. "./priv/static/swagger/index.html"), then create a get route for "/apidocs" pointing at that controller method. – Ilkka Feb 08 '17 at 20:53