by
KK » Thu Mar 16, 2017 1:48 am
As you have noticed, we sure can use Couch tags within <cms:php>.
As to why then the <cms:show> wasn't working as expected in your code, you'll have to understand the execution flow of <cms:php>. Allow me to explain.
The tag works in two passes:
1. First it executes all the Couch tags within its enclosed contents to get a final PHP 'source code' to be evaluated as PHP
2. The output received above is then passed on to PHP interpretor to get the results.
Consider the following example -
- Code: Select all
<cms:php>
<cms:repeat '3'>
echo '<cms:show k_count />';
</cms:repeat>
</cms:php>
Here the <cms:php> tag in the first pass executes all enclosed Couch tags so after this pass the effective PHP code that is finally executed becomes as follows -
- Code: Select all
<cms:php>
echo '0';
echo '1';
echo '2';
</cms:php>
As you can see above, at the point the actual PHP code is executed, there are no Couch tags in there.
With that understood, I think it should be easy to see what is happenning in your code -
Original code -
- Code: Select all
<cms:set target1 = 'initial value' />
<cms:php>
global $CTX;
$var1 = 'new value';
$CTX->set('target1' , $var1);
echo '<br />FAIL Inside the cms:php:<cms:show target1 />';
echo '<br />WORKS Using $CTX in cms:php:' . $CTX->get('target1');
</cms:php>
After first pass -
- Code: Select all
<cms:set target1 = 'initial value' />
<cms:php>
global $CTX;
$var1 = 'new value';
$CTX->set('target1' , $var1);
echo '<br />FAIL Inside the cms:php:initial value';
echo '<br />WORKS Using $CTX in cms:php:' . $CTX->get('target1');
</cms:php>
And that would be the PHP code finally getting executed.
As you can see, the <cms:show> used inside would only print out the value of the variable as it was *before* the $CTX->set code changed it.
Anyway, that said, may I request you to let me know exactly what you are trying to achieve using the <cms:php> tack?
It has its own utility but you can quickly find yourself, as seems to be happening, befuddled trying to juggle two languages together.
For most part, the regular Couch tags suffice. For something not addressed, a better way would be to create a new custom Couch tag yourself.
Take a look at the 'Tips and trick' section and you'll find several addons created by our users that do just this.