Video outline

The Assign tag is used to declare/name a variable.

We can use the assign tag to name a numeric value or a text string. Maybe an easy way to visualise this would be the number Pi. Instead of writing 3.141592653 every single time we could assign this value the name Pie. Once we have assigned this value/variable to the name Pie, we can simply print {{ Pi }} and it will show 3.141592653.

So let’s have a look at how the assign tag can be used in Liquid. The assign tag is written as follows, first we name the variable and after the equal sign we input the content. The content can consist of any sort of value or text but it is important to remember that the text needs to be wrapped within single or double quotation marks.

The assign tag is useful to simplify the code and make it more readable and additionally helps to replicate the same variable with ease. For the first example in this video I will show you how the assign tag functions but first we will need a variable that we will use to assign too.

{% input custom.sf.Whats_your_favourite_number %}

Here we have created an input field where the customer can enter their favourite number. But, what if we wanted to use this value elsewhere? Here is where the assign tag will help you. We can use the assign tag followed by our variable name and the equal(=) sign to assign a numeric value or text string.

In this example we have decided to name the value created in the input field (custom.sf.Whats_your_favourite_number) favourite_number. Make sure the variable is named accurately and is descriptive and understandable to other programmers.

{% assign favourite_number = custom.sf.Whats_your_favourite_number %}

If you wish to reuse this value again you can simply print this value as follows.

{{ favourite_number }}