Rich_Morin

Rich_Morin

I’d like to add HTML content to rendered web pages. My current use case is a table of contents (TOC), listing (and linking to) each of the page’s headings. Here’s some science fiction for a possible approach:

  • grab the output of Phoenix.View.render/3
  • turn the IO List into a single binary string
  • extract the heading levels and text content
  • generate the TOC (e.g., an HTML list of links)
  • add the TOC and anchors to the page’s HTML
  • turn it all back into an IO List

Problem is, I’m not sure about the best way to grab render’s output. Should I override render in some manner, pipe its output into another function, use a pipe_through in router.ex, or what? Suggestions welcome…

Showing Posts 1 to 2

idi527

idi527

:waving_hand:

I’m probably misunderstanding your problem, but wouldn’t it be easier to write a module with functions containing the headings with links and use them both in the pages and in the ToC?

defmodule Headings do
  # not sure it would work

  titles_for_routes = %{
    Routes.page_path(YourWeb.Endpoint, :index) => "This is the Index PAGE",
    # ...
  }

  Enum.map(titles_for_routes, fn {route, title} ->
    def title(unquote(route)), do: unquote(title)
  end)

  def title(_unmatched), do: "Some default title"
end
# on the page
<title><%= Headings.title(@conn.private[:phoenix][:route]) # or whereever phoenix stores it's current route %></title>
# in the toc
<ul>
  <%= Enum.map(@routes, fn route -> %>
    <li><%= link(Headings.title(route), to: route) %></li>
  <% end) %>
</ul>
Rich_Morin

Rich_Morin OP

Thanks for the thoughtful and detailed reply. The problem with this approach, in my case, is that I have a number of places and ways in which headings are generated. For example:

  • Some headings are hard-wired into the templates.
  • Some headings are generated automagically, along with page sections, if the relevant data is available.
  • Some headings are encoded in Markdown source code.

Because I’d rather not couple the TOC generation to each of these spots in the code, I’m thinking about a post-processing approach: if there’s a heading in the resulting HTML, I index it.

Here are some links that may help to show what’s going on:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews