DimWell

DimWell

How to make String with Placeholders?

Hi Experts,

I am looking for built in function something like String builder with Placeholders.
Iet say in C like languages usuall you wold have function that looks like:

print(" Some string %placeholder1, string continues…%placeholder2 ", var1, var2)
#vars goes to placeholders

`Do we have something similar in Elixir ?

Use Case:
User provides: custom url with placeholders and separately details.
Then later url should be combined with data into proper url.

User: 223344
Lang: eng
Type: json | xml

Original URL

http: // somesite. com/?i=#{id}&lang=#{language}&type=api&api_v=1&api_type=#{data_type}"

Updated URL

https: // somesite. com/?i=#223344&lang=#eng&type=api&api_v=1&api_type=#json"

Marked As Solved

cpgo

cpgo

Maybe not the simplest option but also works

iex(1)> template = "http://somesite.com/?i=#<%=id%>&lang=#<%=language%>&type=api&api_v=1&api_type=#<%=data_type%>"
"http://somesite.com/?i=#<%=id%>&lang=#<%=language%>&type=api&api_v=1&api_type=#<%=data_type%>"
iex(2)> EEx.eval_string(template, [id: 1, language: "pt-BR", data_type: "json"])
"http://somesite.com/?i=#1&lang=#pt-BR&type=api&api_v=1&api_type=#json"

Also Liked

michalmuskala

michalmuskala

Format is rather experimental right now and has some issues especially around formatting floats. Maybe I should finally get into it an finish it, now that Jason is mostly done :wink:

jfeng

jfeng

One way is with Michał Muskała’s Format library https://github.com/michalmuskala/format. I’m not sure if there’s any documentation, but you can do:

def print_url(id, language, url) do
  url
  |> Format.compile()
  |> Format.string(id: id, language: language)
  |> IO.puts()
end

where url is a string that contains {id} and {language}, which get replaced by the corresponding values specified in Format.string.

My personal syntactic preference would be

...
  |> Format.string([id: id, language: language])
...
mguimas

mguimas

I suggest you to use :io_lib.format(format, [arg1, ...]) |> to_string(). See the documentation of :io_lib.format.

It is one less dependency if you use this function because it is in the standard library.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement