ShopSite and Global Variables

ShopSite templates often use global variables to store and display values, such as page types (page, product, cart, etc…), layout options (colors, widths, etc…), loop counters and more.

global_no_bgHere are a few important facts about variables and their usage within ShopSite templates:

1.  Variables may contain either numeric or string values.  When assigning a string variable, the value is enclosed by double-quotes.

2.  Variable VALUES are case-sensitive

“Page” and “page” are not considered the same

3.  Variable NAMES are not case-sensitive

[– VAR.somename –] and [– VAR.SomeName –] are considered the same

4.  Variables are set during the publish and remain active throughout generation.  Additionally, the value can change many times before the generation is finished.

5.  Variables are global and available to all objects within the scope of the publish, including loops and other include files.  One common variable found in ShopSite templates is [– VAR.Type –].  This variable takes on different values during a store publish, such as “page”, “more”,  etc….  The variable is used to control output during the generation process.

Below is a quick illustration of the variables life-cycle during a publish.  The variable is declared in the [– DEFINE PAGE –] section of the page template.  Its value is passed into the product loop and can be displayed, evaluated or updated by the product template.  If the value is changed in the product template, the new value is passed back to the page template upon completion of the product loop.  This is particularly important if the variable is used again in the page template after the product loop and requires its value to match the value originally assigned.

variable_flowTo overcome this, you would need to reset the variables value in the page template after the product loop.  Of course, using unique variable names whenever possible helps avoid this type of conflict altogether (especially with custom page/product loop counters).

Variables In Action (Example)

Let’s take a look at two global variables used to control the display of products on a page.  The goal is to create product rows, each having four columns.

[– DEFINE PAGE –]

[– VAR.Counter 0 –]
[– VAR.Columns 4 –]

<table>
[– LOOP PRODUCTS VAR.Columns –]
[– VAR.Counter INC –]
<td>[– PRODUCT –]</td>
[– IF VAR.Counter EQ VAR.Columns –]
[– VAR.Counter 0 –]
</tr><tr>
[– END_IF –]
[– END_LOOP PRODUCTS –]
</table>

[– END_DEFINE PAGE –]

And now the explanation…

[– VAR.Counter –] stores the counter value, which is initially set to zero.

[– VAR.Columns –] stores the number of product columns we intend to display on each row of our page template.

[– VAR.Counter INC –]

With each iteration of the loop, we increment the counter value by 1.

[– IF VAR.Counter EQ VAR.Columns –]
[– VAR.Counter 0 –]
</tr><tr>
[– END_IF –]

When the counter value reaches our column count (4), the previous row is closed out and a new row is triggered.

 

Leave a Reply