Intro to formulas
To manipulate data in your frontend, you can use WeWeb formulas.
Common examples include but are not limited to:
- filtering a list of items,
- formatting text, or
- changing a style based on conditions.
Binding formulas
You can use WeWeb formulas in any binding:
In the example above, you can see:
- we opened the binding explorer,
- went to the formula tab to look for the pre-built
switch
formula, and - wrote a formula directly in the explorer to format the text on the page based on the value of item in our collection.
This formula is local in the sense that, if we wanted to use it elsewhere in our app, we would have to re-write or copy/paste it.
Create a global formula
As your app grows in complexity, we recommend you build global formulas in the Actions
tab that you can then use throughout your app:
In the example above, you can see:
- in the
Actions
tab, - we created several global formulas,
- the
formatNumber_nocode
formula takes aText
input that expects a number - in the binding explorer, global formulas can be found in the
From project
section of the formula tab
Bind to a global formula
Global formulas are helpful because you can use them everywhere:
In the example below, you can see:
- we opened the binding explorer to a text element,
- went to the formula tab,
- used our global
formatNumber_nocode
formula, and - built on top of it to format additional text.
Global vs local formulas
In some cases, it makes sense to create formulas that are scoped to a specific element or component.
In other cases, it's helpful to create global formulas that you can reuse throughout your app.
As a general rule, if you catch yourself re-writing or copy/pasting formulas multiple times, it's a sign you might want to create a global formula instead because, if you need to debug or improve it down the line, you can do it in one place.
In addition, using global formulas will allow you to see which ones are being used on a page: