Eex if else for dev or prod with releases?

I read from here that MIX is not available in releases so code like this wont work in releases

    <%= if Mix.env == :dev do %>
      <a class="title" href="http://<%= post.website.dev_url %>/p/<%= post.id %>-<%= post.slug %>"><img class="card-img-top img-fluid" alt="<%= post.intro %>" src="<%= Application.fetch_env!(:arc, :primary) %><%= WwwSiteCom.Web.Blogs.Post.Image.url(post.filename, :card) %>"></a>
    <% else %>
      <a class="title" href="http://<%= post.website.domain %>/p/<%= post.id %>-<%= post.slug %>"><img class="card-img-top img-fluid" alt="<%= post.intro %>" src="<%= Application.fetch_env!(:arc, :primary) %><%= WwwSiteCom.Web.Blogs.Post.Image.url(post.filename, :card) %>"></a>
    <% end %>

I read a stackoverflow about creating a macro but am wondering if there is any help on offer regarding a EEX example

ideally I want this switching, perhaps I need a function in a view?

I should also add, this is a multisite umbrella app

I understand the normal working of dev and prod

I include .domain and .dev_url fields because I am cross-linking from one domain to another

I am using forwards in the router to allow re-use of controllers/views/templates between sites (umbrellla apps)

Use a config that has either one URL or the other.

As mentioned, adding the domains to the config would be a good solution.

For things that don’t go cleanly into the config or where you need to run different code paths based on Mix env, I use this in my config.exs:

# Store mix env used to compile app, since Mix won't be available in release
config :code_stats, compile_env: Mix.env()

This way I can use Application.get_env(:code_stats, :compile_env) and it will work inside and outside releases, and will reflect the env the app was compiled with.

2 Likes

yes, I do that

my problem is that posts are associated with websites/domains

so, that my posts on site 1, has links to site 2, 3, 4

so, I need to do the db lookup

what I have done as workaround is a special exs for prod that changes the values from local.site.com:4000 to site.com

I’ll backfill this post later for better effect, but I have a workaround

Can’t this just be replaced with this though?

    <%= if unquote(Mix.env() == :dev) do %>

Still, testing specific environment names sounds like very poor form though, wouldn’t it be better to put a binding in a config file and just access that instead?

1 Like