Share feedback, ideas and get community help

Updated 12 months ago

Formating

I'm using the code bellow every time I want to display a monetary variable:
Plain Text
{{= {{variable}}.toLocaleString('pt-br', { style: 'currency', currency: 'BRL'}) =}}

Is there a better way to do this?
B
V
A
21 comments
Is there something wrong with it?
It looks good to me!
Oh if you use it several time then I suggest you set it on another variable using the Set Variable block: https://docs.typebot.io/editor/blocks/logic/set-variable
Nothing wrong. Just repetitive.
There is a place where I can define custom functions?
I tryed, with no success, define a function inside a script block.
What I imagine that could be possible is something like:
Script block: const r = (x) => x.toLocaleString('pt-br', { style: 'currency', currency: 'BRL'})
Text block: {{= r(123456) =}}
Have you read the Set variable doc? Can you let me know what is unclear?

All you have to do is add a Set variable block, select Custom value and paste: {{variable}}.toLocaleString('pt-br', { style: 'currency', currency: 'BRL'})
My goal is not to store a formatted value.
My goal is to define a format function that I can reuse later with different variables.
I'm using many monetary variables, that change values over the course of the conversation.
So, at the moment, this formatting code is replicated over an over in different blocks.
That's what I'm trying to avoid.
Just compute everything in several variables and finally have a block that generates the end content?
Let me reformulate.
The question is:
Is it possible to have user defined functions?
I guess not, right?
what @Vilela™ is trying to say is if its possible to create a global function in one of the script block that will be accessible by all the other blocks

is that correct?
It don't have to be in the script block, can be anywhere.
But yes, you got the point. 👍
What you think you need can be wrong so I’m trying to understand the issue you have so that I can provide the best solution 🙂
That is not possible
Got it. Thank you.
Hey so sorry @Vilela™, I actually thought I was an another thread so my answers don't make sense 🙏
Figured it out:
Attachment
image.png
save your function in the window at the start (make sure to execute it in client)
Attachment
image.png
then just call it in any of the set variable block after it.
Attachment
image.png
Issue with this work around?
Has to be run on client

Solution proposed for Baptiste:
Allow adding a script block in typebot settings where the script will be automatically added to the viewer file
Add a reply
Sign up and join the conversation on Discord