streamline the feed controller with put_root_layout plug, write post about it
This commit is contained in:
parent
9291bdb88c
commit
662e85da6c
4 changed files with 37 additions and 12 deletions
|
@ -1,5 +1,7 @@
|
||||||
defmodule Home73kWeb.FeedController do
|
defmodule Home73kWeb.FeedController do
|
||||||
use Home73kWeb, :controller
|
use Home73kWeb, :controller
|
||||||
|
plug :put_layout, false
|
||||||
|
plug :put_root_layout, false
|
||||||
|
|
||||||
alias Home73k.Blog
|
alias Home73k.Blog
|
||||||
|
|
||||||
|
@ -9,7 +11,6 @@ defmodule Home73kWeb.FeedController do
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_resp_content_type("application/rss+xml")
|
|> put_resp_content_type("application/rss+xml")
|
||||||
|> put_layout(:false)
|
|
||||||
|> render("rss.xml", posts: posts, last_build_date: last_build_date)
|
|> render("rss.xml", posts: posts, last_build_date: last_build_date)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<%= if length(post.tags) > 0 do %>
|
<%= if length(post.tags) > 0 do %>
|
||||||
<div class="post-tags fs-smaller mb-3">
|
<div class="post-tags fs-smaller mb-4">
|
||||||
<%= icon_div @socket, "mdi-tag-multiple", [class: "icon baseline text-gray-300"] %>
|
<%= icon_div @socket, "mdi-tag-multiple", [class: "icon baseline text-gray-300"] %>
|
||||||
<%= for {tag, i} <- Enum.with_index(post.tags) do %>
|
<%= for {tag, i} <- Enum.with_index(post.tags) do %>
|
||||||
<span class="text-gray-300">#</span><%= live_redirect tag, to: Routes.blog_path(@socket, :tag, tag) %><%= i < (length(post.tags) - 1) && "," || "" %>
|
<span class="text-gray-300">#</span><%= live_redirect tag, to: Routes.blog_path(@socket, :tag, tag) %><%= i < (length(post.tags) - 1) && "," || "" %>
|
||||||
|
|
|
@ -3,7 +3,7 @@ defmodule Home73kWeb.Router do
|
||||||
alias Home73kWeb.CSPHeader
|
alias Home73kWeb.CSPHeader
|
||||||
|
|
||||||
pipeline :browser do
|
pipeline :browser do
|
||||||
plug :accepts, ["html"]
|
plug :accepts, ~w(html xml)
|
||||||
plug :fetch_session
|
plug :fetch_session
|
||||||
plug :fetch_live_flash
|
plug :fetch_live_flash
|
||||||
plug :put_root_layout, {Home73kWeb.LayoutView, :root}
|
plug :put_root_layout, {Home73kWeb.LayoutView, :root}
|
||||||
|
@ -12,10 +12,6 @@ defmodule Home73kWeb.Router do
|
||||||
plug CSPHeader
|
plug CSPHeader
|
||||||
end
|
end
|
||||||
|
|
||||||
pipeline :xml_rss do
|
|
||||||
plug :accepts, ["xml", "rss", "atom"]
|
|
||||||
end
|
|
||||||
|
|
||||||
pipeline :api do
|
pipeline :api do
|
||||||
plug :accepts, ["json"]
|
plug :accepts, ["json"]
|
||||||
end
|
end
|
||||||
|
@ -36,13 +32,9 @@ defmodule Home73kWeb.Router do
|
||||||
live "/blog/page/:page", BlogLive, :page
|
live "/blog/page/:page", BlogLive, :page
|
||||||
live "/blog/tag/:tag", BlogLive, :tag
|
live "/blog/tag/:tag", BlogLive, :tag
|
||||||
live "/blog/:id", BlogLive, :show
|
live "/blog/:id", BlogLive, :show
|
||||||
end
|
|
||||||
|
|
||||||
scope "/feed", Home73kWeb do
|
|
||||||
pipe_through :xml_rss
|
|
||||||
|
|
||||||
# Feeds
|
# Feeds
|
||||||
get "/", FeedController, :rss
|
get "/feed", FeedController, :rss
|
||||||
end
|
end
|
||||||
|
|
||||||
# Other scopes may use custom stacks.
|
# Other scopes may use custom stacks.
|
||||||
|
|
32
priv/content/2021/04/2021-04-07_rss-feed-phoenix-liveview.md
Normal file
32
priv/content/2021/04/2021-04-07_rss-feed-phoenix-liveview.md
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
---
|
||||||
|
%{
|
||||||
|
title: "RSS Feed When Using Elixir Phoenix LiveView",
|
||||||
|
id: "rss-feed-elixir-phoenix-liveview",
|
||||||
|
date: ~N[2021-04-07 12:31:00],
|
||||||
|
author: "Adam Piontek",
|
||||||
|
tags: ~w(tech elixir phoenix liveview coding routing rss feed development)
|
||||||
|
}
|
||||||
|
---
|
||||||
|
|
||||||
|
While [re-implementing my website in elixir/phoenix](/blog/blog-incorporated-2021), I wanted to include an RSS feed for the blog posts. Luckily I found pretty much everything I needed in Daniel Wachtel's [Building an RSS Feed with Phoenix](https://danielwachtel.com/phoenix/building-rss-feed-phoenix) post, but since I'm making use of LiveView, I ran into one hiccup --- errors about a missing `root.xml` layout!
|
||||||
|
|
||||||
|
<!--more-->
|
||||||
|
|
||||||
|
LiveView changes how Phoenix handles layouts --- there's a `root.html.leex` layout, and then for live views, a `live.html.leex` sub-layout, and for regular controller views, an `app.html.eex` sub-layout.
|
||||||
|
|
||||||
|
Without LiveView, Daniel's controller directive `plug :put_layout, false`{:.lang-elixir} would be enough, but with LiveView, Phoenix is still looking for the *root* layout template, which for my `index.html` would be `root.xml` ... what to do?
|
||||||
|
|
||||||
|
One option would be to create a whole separate router pipeline to skip the `plug :put_root_layout, {Home73kWeb.LayoutView, :root}`{:.lang-elixir} directive. It would work, but is a heavier approach.
|
||||||
|
|
||||||
|
But luckily, there's a new [put_root_layout/2](https://hexdocs.pm/phoenix/Phoenix.Controller.html#put_root_layout/2) that we can leverage like so:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
defmodule YourAppWeb.RSSController do
|
||||||
|
use YourAppWeb, :controller
|
||||||
|
plug :put_layout, false
|
||||||
|
|
||||||
|
...
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
With this in place, the rss xml is served plain, just like we want.
|
Loading…
Reference in a new issue