This is a very little known topic so KK's detailed reply should be helpful.

I was testing following piece:

Code: Select all

<cms:embed "test.html" my_var='my_var' >
    <cms:set result-0 = 'result-0' scope='local'/>
    <cms:set result-1 = 'result-1' scope='parent'/>
    <cms:capture into='result-2' scope='parent'>
        result-2
    </cms:capture>
</cms:embed>



The content of "test.html" snippet contained only dump to see placement of variables: "<cms:dump_all />"

2018-03-07-001.png
2018-03-07-001.png (3.24 KiB) Viewed 282 times

And output doesn't have result-2, so I asked KK about it.





KK wrote: Hi,

The 'parent' scope expects the variable being set to already exist in the scopes of any of the current tag's parent. If no such variable is found, the value is set as normal scope variable. The comment in tags.php for <cms:set> mentions this -
/*
'set' by default will set a variable only in the immediate scope (first scoped tag encountered)
However if 'parent' is specified as second param, it searches
upwards through the hierarchy and sets the variable at any parent's
scope it finds it.
If not found anywhere, the var is set at the default scope.


If 'global' is set, the var is set at the root scope.
*/

With that understood, the behavior of your code can be explained -

While setting 'result-1', the current tag is 'cms:set', so Couch begins searching for just above it - it searches 'embed' and then 'root'. Does not find a variable of this name at both places so switches to default (i.e. set into the first found tag that supports scope). cms:set itself does not support scope so the first tag that does is cms:embed and that is where the variable is set.

With 'result-2', the current tag is cms:capture. The same process as above is repeated. The variable is not found in any of the parents so the default is used. The first tag with scope this time though happens to be cms:capture itself and that is where the value is set.

Of course, cms:capture tag ends soon afterwards and the variable gets lost when control comes back to embed.

If you are still with me, the correct way of using 'parent' scope is to explicitly define a variable (blank will do) in the parent where you want that variable to be set. If not, the results can be indeterminate, as you just saw.

In your case it is the embed tag, so the code should be as follows -
Code: Select all
<cms:embed "test.html" my_var='my_var' >
    <cms:set result-0 = 'result-0' scope='local'/>
    <cms:set result-1 = 'result-1' scope='parent'/>
    <cms:set result-2 = '' />
    <cms:capture into='result-2' scope='parent'>
        result-2
    </cms:capture>
</cms:embed>

Hope it helps.

Regards