Change global variable from snippet template

I am trying to automatically display content in a two column table where it toggles from the first column to the second and so forth based upon a global variable and the logic I have in the snippet template. I can read the global variable from the page template, but I cannot change it in the snippet template. See below.


##---- START PAGE TEMPLATE ----
#set($column = 1)
#slot("ogi_MultimediaColumnsAuto" "<table>" "" "" "</table>" "")
##---- END PAGE TEMPLATE ----


##---- START SNIPPET TEMPLATE ----
#if($column != 2)
<tr>
#end

<td width="50%" valign="top">$Displaytitle</td>

#if($column == 2)
</tr>
#end

#if($column == 1)
  #set($column = 2)
#else
  #set($column = 1)
#end
##---- END SNIPPET TEMPLATE ----

This thread should probably be in the technical discussion forum as you’re asking for code as opposed to sharing working code…

However,

The solution you’re looking for is not to use a global variable, but instead, use $sys.index.

your snippet code should look like this:

##---- START SNIPPET TEMPLATE ----
#set($index = $tools.math.toInteger("0$!{sys.index}") )## gets the index, but uses 0 if we're previewing the snippet by itself.
#set($columnmod = $tools.math.mod( $index, 2 )
#if($columnmod == 1)
<tr>
#end

<td width="50%" valign="top">$Displaytitle</td>

#if($columnmod == 0 && $index > 0)
</tr>
#end
##---- END SNIPPET TEMPLATE ----

Notice: Now, there’s only 1 place where the number of columns is actually specified, and the rest of the template doesn’t care how many columns there will be… this leaves you open to having the number of columns becoming a value set in an HTML parameter that you can pull from $sys.params… so your template becomes even that much more re-usable.