1

I am currently looking to figure the scope of #assign variable. However, scope seems to be confusing please see following example and try to explain. I have some hypothesis which works but if somebody has concrete documentation, it will be very helpful.

<#assign test1 = a.test1/>
<#if some_condition>
    <#if a.test1?contains(",")>
        <#assign mylist = a.test1?split(",")/>
        <#assign test1 = mylist?size-1?int/>
    </#if>
    <!-- Line is giving me exception, if I convert everything to #global it works -->
    <#assign testing>${test1} SAJNF</#assign>
</#if>

Can somebody explain scope in #assign and #global?

Subodh Gupta
  • 193
  • 1
  • 3
  • 12

1 Answers1

2

That example should work equally well with #assign and #global. You had some kind of oversight there.

As of #assign VS #global: Variables set via #assign are only visible from the template where they were assigned, plus in the templates that where #include-d in that template (or #include-d in an #include-d template, etc.), because #include-ing is pretty much like copy-pasting. But if you #import a template, as that will have its own namespace, it won't share #assign-ed variables with the #import-ing template. It will share the global variables with it though; variables set with #global are seen from every templates, just like the data-model. You should hardly ever use #global BTW.

ddekany
  • 29,656
  • 4
  • 57
  • 64