Help on handling breadcrumbs in phoenix

I’m thinking about how to create a good workflow for breadcrumbs in my app.

Right now I have the breadcrumbs directly in the HTML

<span><a href="<%= Routes.static_path(@conn, "/my_page") %>">A title for the page</a></span>

But I’d like to have them available from within the layout so I can set the breadcrumbs in a script tag for JSON LD.

I think this would mean creating the breadcrumbs in the controller method and then setting them in the assigns.

Is there a way to do this within the html as it would be nice to be able to set them there, but have them picked up earlier in the rendering for the JSON LD bit from the layout. I don’t know if there’s a way to do something like that.

Thanks for any help.

Hello and welcome,

You can create helpers for rendering. For example, just add a /lib/app_web/helpers/helper.ex

Then reference it in the web config file, to be able to use it everywhere…

This could be like.

<%= breadcrumb(title, path) %>

Don’t put this logic in controller, it’s not the right place… I think.

Hi @bapti, welcome to the forum! :smile:

Have you looked at breadcrumble_ex yet?
Maybe you can take some inspiration from there.

2 Likes