Named routes with sigil_p

I have an umbrella project with N apps, all with the same web structure, but different db and possibly language.

Since the templates are all the same, I thought of creating a common app and I’m using templates like this:

defmodule WebEnglish.CategoryHTML do
  use WebEnglish, :html

  embed_templates "category_html/*", root: Common.Templates.root()


defmodule WebSpanish.CategoryHTML do
  use WebSpanish, :html

  embed_templates "category_html/*", root: Common.Templates.root()

I’m now having two issues:

  • I can’t use verified routes from common modules that are not templates, since sigil_p is not available there.
  • I can use verified routes in templates within common, but what if I need routes in another language/format? ex. ~p"/categories" works, but in WebSpanish this should be ~p"/categorias".

I’m looking at localized routes, but this wouldn’t work either because my understanding is that both categories and categorias would be valid routes in both my web apps.

So I kind of need Router Helpers again, but I know it’s deprecated and I’m looking for an alternative.
What do you think? Thanks!

You can just import them assuming it all uses the same router. Recent Phoenix generators even give you a function to do so:

use MyAppWeb, :verified_routes

Take a look in your lib/my_app_web.ex file to see what that does (look for def verified_routes).

I have no experience localizing Elixir apps yet but there is this project which is by @kip so it’s gotta be good :slight_smile:

I can’t do that because MyAppWeb inside Common changes (it’s either WebEnglish or WebSpanish).

Ah right. Router helpers would have the same problem, though. Do you separate routers for each language are only one? If you have two then I’m not sure what the answer is here. If you have one then you can still use verified routes.

Router helpers work, I can do this in the WebEnglish.router function (same for spanish):

 use Phoenix.Router, helpers: true
 import WebEnglish.Router.Helpers

And in my templates (which are in the Common app):

<a href={category_path(@conn, :index)}>Categories</a>

To answer your question: yes, routers are separate per app (since the url structure and/or the languages are different).

Oh interesting, so the WebEnglish router helpers also work with the Spanish routes?

No, no, I have to duplicate that snippet in WebSpanish, too.

Can you give an example of how you are hoping a shared template will look? Your whole second question of “what if I need routes in another language” is I guess where I’m also confused.

I think you may want to look how the project I linked does it because I don’t think having one web module per language is the best course of action, even if you are only ever going to have two of them. If you do want to have two, I can’t really think of a better way than creating macros that check which module they are in and return the appropriate routes. This would be a 1-1 macro to routes, though which means basically you’d essentially have a third place to manage routes. Maybe someone else will have a better idea. If I think of anything else I’ll report back!

I’d like my template to look like this:

<a href={category_path(@conn, :index)}>Categories</a>

So, basically, I’d like the helper back :slight_smile: As stated before, this works and I know I can use it now, but it’s deprecated and I was wondering if there was an alternative.

Sorry about the misunderstanding with languages: my project has a single template for many frontends. They don’t necessarily have different languages, but they’re deployed separately and have their own db. The only thing in common are templates. Routes may or may not be different for the same entities, that’s why I need separate routers.

After reading the thread I am not sure I understand but let’s kick the can…

I think you don’t necessarily want helpers back but want to be able to write a path in your templates which translates during runtime to an URL depending on certain criteria.

Have a look at GitHub - BartOtten/routex. Unlike native verified routes, Routex Verified Routes does support branching. As Routex is extension based, I am quite sure there is a combination of extension which will support your use case.

of single path in template with variable path during runtime

source code
demo page

Thank you, this looks really promising!