How to call functions from a .eex file

I am trying to learn Phoenix and wondered. In Rails, I could say

<%= Rails.version %> 

to get the current version of the rails displayed in the .erb file. I asked how to do this in Phoenix and got an answer

 :application.get_key(:phoenix, :vsn) 

Unfortunately, this underlines my ignorance when I try to grow with Phoenix. When i put

 <%= :application.get_key(:phoenix, :vsn) %> 

in my .eex file, I get

 no function clause matching in Phoenix.HTML.Safe.Tuple.to_iodata/1 

Please indicate to me any documents that will help me find out what to do next. Thanks!

+5
source share
1 answer

The call :application.get_env returns a tuple in the format:

 {:ok, '1.0.0'} 

Phoenix.HTML.Safe does not have a function to decode a tuple in this format ( source ). You will need to extract the version from the call:

 <%= :application.get_key(:phoenix, :vsn) |> elem(1) %> 

However, a better way would be to use a helper function:

 defmodule VersionHelper do def version do case :application.get_key(:phoenix, :vsn) do {:ok, vsn} -> vsn _ -> #raise or return null or something else end end end 

This can then be called in your view using VersionHelper.version - this means that the version selection is not tied to the key that the phoenix uses in the view.

+8
source

Source: https://habr.com/ru/post/1233002/


All Articles